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

当前回答

如果你在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。

谢谢

其他回答

我必须实现带有工具栏滚动的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>

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

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

有很多很好的答案。关键是你必须将nestedScrollingEnabled设置为false。如上所述,你可以在java代码中做到这一点:

mRecyclerView.setNestedScrollingEnabled(false);

但你也有机会在xml代码中设置相同的属性(android:nestedScrollingEnabled="false"):

 <android.support.v7.widget.RecyclerView
     android:id="@+id/recyclerview"
     android:nestedScrollingEnabled="false"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />

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

https://stacklearn.ir

我已经使用了这个很棒的扩展(写在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