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

当前回答

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

其他回答

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

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

RecyclerView

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

嵌套滚动视图

android:fillViewport=“true”

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

尝试使用这个库- https://github.com/serso/android-linear-layout-manager。

库的LayoutManager使RecyclerView包装其内容。在这种情况下,RecyclerView将“与内部视图一样大”,因此它将没有滚动条,用户将使用NestedScrollView的滚动能力。因此,它不会像“scrollable内部scrollable”那样模棱两可。

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

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

如果您使用的是RecyclerView-23.2.1或更高版本。下面的解决方案可以很好地工作:

在你的布局中添加这样的RecyclerView:

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

在你的java文件中:

RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
layoutManager.setAutoMeasureEnabled(true);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(new YourListAdapter(getContext()));

这里layoutManager.setAutoMeasureEnabled(真正的);会成功的。

查看这个问题和这个开发者博客以获得更多信息。