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

当前回答

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

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

其他回答

您需要使用23.2.0(或)以上版本的支持库 和RecyclerView高度将wrap_content。 recyclerView.setNestedScrollingEnabled(假)

但是这样循环模式就行不通了。(即所有的视图将被一次加载,因为wrap_content需要完整的RecyclerView的高度,所以它将一次绘制所有的子视图。没有视图将被回收)。除非确实需要,否则尽量不要使用这种模式。尝试使用viewType并添加所有其他需要滚动到RecyclerView的视图,而不是在Scrollview中使用RecyclerView。性能影响将非常大。

为了让它变得简单"它就像所有子视图的LinearLayout一样"

如果你在NestedScrollView中使用RecyclerView ScrollListener, addOnScrollListener监听器不能正常工作。

使用这段代码。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState); 
              ......
        }
    });

这段代码工作良好的RecyclerView ScrollListener内的NestedScrollView。

谢谢

如果你想在NestedScrollView中使用RecyclerView,这是一个简单的技巧,只需设置:

RecyclerView

recyclerView.setHasFixedSize (false) (java / kt) android: nestedScrollingEnabled = " false " android: layout_height = " wrap_content " android: overScrollMode =“从不”

嵌套滚动视图

android:fillViewport=“true”

这是我的工作,你可以使用许多RecyclerView在NestedScrollView与此。

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

我必须实现带有工具栏滚动的CoordinatorLayout,这花了我一整天的时间。我已经通过移除NestedScrollView工作了。我只是在根处使用RelativeLayout。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</RelativeLayout>