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

当前回答

对于androidx,它被称为androidx.core.widget. nestedscrollview -它像黄油一样滚动,属性isScrollContainer和measureAllChildren启用:

<!-- Scrolling Content -->
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:isScrollContainer="true"
    android:measureAllChildren="true"

    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fastScrollEnabled="true"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical"
        android:splitMotionEvents="false"
        android:verticalScrollbarPosition="right"/>

</androidx.core.widget.NestedScrollView>

其他回答

至少早在Material Components 1.3.0-alpha03中,RecyclerView是否嵌套并不重要(在ScrollView或NestedScrollView之外的东西中)。只要把app:layout_behavior="@string/appbar_scrolling_view_behavior"放在它的顶层父元素上,它是CoordinatorLayout中的AppBarLayout的兄弟元素。

当我在Jetpack导航中使用单个活动架构时,这已经为我工作了,其中所有片段都共享来自活动布局的相同AppBar。我将FragmentContainer作为CoordinatorLayout的直接子,它也包含AppBarLayout,如下所示。各个片段中的RecyclerViews正常滚动,AppBar折叠起来并按预期重新出现。

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:defaultNavHost="true"
            app:navGraph="@navigation/mobile_navigation"/>

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:liftOnScroll="true">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:layout_scrollFlags="scroll|enterAlways|snap" />

        </com.google.android.material.appbar.AppBarLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

liftOnScroll(用于应用程序栏看起来像他们有零海拔时,在页面的顶部)工作,如果每个片段传递其RecyclerView的ID到AppBarLayout。liftOnScrollTargetViewId in Fragment.onResume。如果Fragment没有滚动,则传递0。

我在NestedScrollView里面有Viewpager和RecyclerView。在添加以下行之后

recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);

我解决了滚动缓慢和滚动延迟的问题。

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

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,它为我工作。我必须记住的唯一问题是NestedScrollView只接受一个子视图。所以在我的情况下,我使用的LienearLayout视图组,它是住房我的RecyclerView加上其他一些我需要的视图。

我经历了一个问题,把我的RecyclerView内的NestedScrollView。我意识到我的RecyclerView的内容滚动变慢了。

我后来意识到我的RecyclerView正在接收滚动事件,因此与NestedScrollView的滚动行为相冲突。

所以为了解决这个问题,我不得不禁用我的RecyclerView的滚动功能与这个方法movieListNewRecyclerView.setNestedScrollingEnabled(false);

你可以在我的Instagram上查看我实际做了什么。这是我的instagram账号elix03

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