如何在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>

当前回答

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

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

其他回答

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

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

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

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

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

我已经使用了这个很棒的扩展(写在kotlin,但也可以在Java中使用)

https://github.com/Widgetlabs/expedition-nestedscrollview

基本上你在任何包中都有NestedRecyclerView比如在项目中的utils,然后创建你的recyclerview

 <com.your_package.utils.NestedRecyclerView
      android:id="@+id/rv_test"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

看看Marc Knaup写的这篇很棒的文章

https://medium.com/widgetlabs-engineering/scrollable-nestedscrollviews-inside-recyclerview-ca65050d828a