如何在NestedScrollView内使用RecyclerView ? 设置适配器后,RecyclerView内容不可见。

更新布局代码。

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/keyline_1">

    </RelativeLayout>

    <View
        android:id="@+id/separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#e5e5e5" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/conversation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

当前回答

不能在嵌套滚动视图中使用回收器视图。它并不打算包含更多的可滚动视图,但因为它是滚动布局本身的子视图,所以你需要嵌套的滚动视图。我有同样的问题,但在最后我移动我的textview是一个头视图内的recyclerview,使recyclerview的协调器布局和删除嵌套滚动视图的直接子。然后我所有的问题都消失了。

其他回答

保持recyclerview的回收特性并避免recyclerview加载所有数据的一个解决方案是在recyclerview本身设置一个固定的高度。通过这样做,recyclerview只能加载它的高度所能显示给用户的东西,这样当你滚动到底部/顶部时,循环它的元素。

有一个简单的测试代码,你可以检查

<android.support.v4.widget.NestedScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fillViewport="true"
     app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v7.widget.RecyclerView
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>
   </android.support.v4.widget.NestedScrollView>

下面是我用来避免滚动问题的代码:

mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.getLayoutManager().setAutoMeasureEnabled(true);
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(false);

不要在NestedScrollView中使用recyclerView。这可能会导致级联问题! 我建议使用ItemViewTypes在RecyclerView处理多种类型的视图。 只需添加一个RecyclerView与match_parent宽度和高度。然后在你的recyclerViewAdapter覆盖getItemViewType和使用position处理什么布局被膨胀。之后,你可以使用onBindViewHolder方法来处理你的视图持有者。

https://stacklearn.ir

你可以使用android:fillViewport="true"来让NestedScrollView测量RecyclerView。RecyclerView将填充剩余的高度。所以如果你想滚动NestScrollView,你可以设置RecyclerView的minHeight。