首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >包含CardView的RecyclerView不能滚动(不在焦点中?)除非在单击一次之后

包含CardView的RecyclerView不能滚动(不在焦点中?)除非在单击一次之后
EN

Stack Overflow用户
提问于 2016-09-07 10:02:52
回答 2查看 666关注 0票数 0

我的RecyclerView包含CardView的列表

用于MainActivity的xml:

代码语言:javascript
运行
复制
<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>
            <android.support.v7.widget.Toolbar/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/view_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>
    </android.support.v4.widget.NestedScrollView>

    <FloatingActionButton/>

</android.support.design.widget.CoordinatorLayout>

我使用上述RecyclerView的适配器来容纳这些卡。

用于在适配器内膨胀ViewHolder的xml:

代码语言:javascript
运行
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relativeLayout"
    android:paddingTop="2dp"
    android:paddingRight="2dp"
    android:paddingLeft="2dp"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">


    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardBackgroundColor="@android:color/holo_red_light"
        card_view:cardPreventCornerOverlap="true"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="3dp"
        card_view:contentPadding="7dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:id="@+id/relat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:focusable="true"  
            android:focusableInTouchMode="false"
            android:padding="10dp">

            <TextView/>
            //...

        </RelativeLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>

为了让卡片可以点击,我在these - two流行的帖子中尝试了所有的解决方案,但我总是有这个奇怪的bug:

当我第一次启动应用程序时,卡片列表不会滚动,除非我点击一次RecyclerView。这就好像RecyclerView一开始并不在焦点上。

另外,如果我去掉了所有的单击侦听器或类似的方法来使CardView的代码可单击,而只保留xml格式的可聚焦代码:

代码语言:javascript
运行
复制
           android:focusable="true"  
           android:focusableInTouchMode="false"

,然后它确实会立即滚动,但只要我添加任何单击(侦听器)机制,或者甚至为ViewHolder添加"android:clickable="true"“,该错误就会重新出现。请给我建议。谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-09 09:19:16

事实证明,滚动问题与RecyclerView无关。这是由于我使用的一个开源小工具,它锚定在RV上,并以某种方式干扰了聚焦/滚动/触摸拦截。经过几天的寻找,终于摆脱了这个bug ..

不管怎样还是要谢谢你

票数 0
EN

Stack Overflow用户

发布于 2016-09-07 10:30:03

您永远不应该在ScrollView中嵌套RecyclerView。只需移除NestedScrollViewRecyclerView就会处理好它的滚动行为。

代码语言:javascript
运行
复制
<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>
            <android.support.v7.widget.Toolbar/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/view_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39360207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档