包含 CardView 的 RecyclerView 不会滚动(不在焦点上?)除非点击一次

RecyclerView containing CardView's won't scroll (not in focus?) unless after clicked once

我的 RecyclerView 包含一个 CardView 列表

xml 对于 MainActivity:

<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 使用适配器来包含卡片。

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

<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 热门帖子中的所有解决方案,但我总是遇到这个奇怪的错误:

当我第一次启动应用程序时,卡片列表不会滚动,除非我单击一次 RecyclerView。好像 RecyclerView 最初没有聚焦。

此外,如果我摆脱所有点击侦听器或类似的方式使 CardView 可点击,并且只保留 xml 中的可聚焦代码:

           android:focusable="true"  
           android:focusableInTouchMode="false"

,然后它会立即滚动,但是只要我添加任何点击(侦听器)机制,甚至为 ViewHolder 添加 "android:clickable="true"",该错误就会重新出现。 请指教。谢谢

您永远不应将 RecyclerView 嵌套在 ScrollView 中。只需删除 NestedScrollViewRecyclerView 应该会处理其滚动行为。

<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>

原来滚动问题与 RecyclerView 无关。这是由于我使用了一个开源小部件,它锚定到 RV 并以某种方式干扰了 focusing/scrolling/touch 拦截。经过几天的寻找别处,终于摆脱了这个错误..

还是谢谢大家