RecyclerView 不嵌套在 NestedScrollView 中但在同一个 LinearLayout 中时不滚动

RecyclerView doesnot scroll when not nested in NestedScrollView but is in the same LinearLayout

我正在使用以下布局,但无法让 RecyclerView 滚动(使用此布局时在屏幕上不可见,直到 NestedScrollView 停止滚动)。

我可以向上滚动到 NestedScrollView 和 CollapsingToolbar 以折叠,如果我删除整个 NestedScrollView 然后我让 RecyclerView 滚动。

如果我在没有 NestedScrollView 的情况下保持线性布局,只有 RecyclerView 滚动,其余布局是固定的。

我还向 RecyclerView 添加了 app:layout_behavior="@string/appbar_scrolling_view_behavior",并且将 RecyclerView 排除在 NestedScrollView 之外。

如果我在 NestedScrollView 中添加 RecyclerView,RecyclerView 不会出现。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.MainFragment">

    <!-- android.support.design.widget.AppBarLayout here
         with a android.support.design.widget.CollapsingToolbarLayout
    -->

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp">

                    <!-- more layout code here -->
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/large_text"/>

                </RelativeLayout>



                <View
                    android:id="@+id/separator"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/colorAccent" />

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewListOfData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:listitem="@layout/recycler_view"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </LinearLayout>

好的,如果您想在 NestedScrollView 中添加 RecyclerView,请将此行添加到 xml 文件 app:layoutManager="LinearLayoutManager" 的 RecyclerView 中 app:layoutManager="LinearLayoutManager"

Example

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/your_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="LinearLayoutManager"/>

    </android.support.v4.widget.NestedScrollView>

然后在您填充 RecyclerViewSomeActivity.java 文件中放置此行 recyclerView.setNestedScrollingEnabled(false);,然后再将适配器设置为 RecyclerView

Example

RecyclerView recyclerView=(RecyclerView)findViewById(R.id.your_recyclerview);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(yourAdapter);

我通过在 NestedScrollView 中嵌套 RecyclerView 并更新 recyclerview

的支持库解决了这个问题

我正在使用 com.android.support:recyclerview-v7:23.0.1

截至 Android 支持库,修订版 23.2.0(2016 年 2 月) (refer revision archive here)

Changes for v7 RecyclerView library:

RecyclerView now has an opt-in feature called AutoMeasure which allows RecyclerView.LayoutManager to easily wrap content or handle various measurement specifications provided by the parent of the RecyclerView. It supports all existing animation capabilities of the RecyclerView.

If you have a custom RecyclerView.LayoutManager, call setAutoMeasureEnabled(true) to start using the new AutoMeasure API. All built-in RecyclerView.LayoutManager objects enable auto-measure by default.

RecyclerView.LayoutManager no longer ignores some RecyclerView.LayoutParams settings, such as MATCH_PARENT in the scroll direction.

Note: These lifted restrictions may cause unexpected behavior in your layouts. Make sure you specify the correct layout parameters.

使用com.android.support:recyclerview-v7:23.4.0解决了nested recyclerview不出现在nested scroll view中的问题