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

当前回答

您可以使用我的示例代码

    <?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <LinearLayout
        android:id="@+id/fl_all_brand"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".view.fragment.AllBrandFragment">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <LinearLayout
                android:id="@+id/fl_all_brand1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:orientation="vertical">


                <!--<include layout="@layout/content_home" />-->

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginStart="@dimen/_15sdp"
                    android:layout_marginTop="@dimen/_20sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/DISPLAY_LIGHTS"
                    android:textColor="@color/gray_scale_placehold"
                    android:textSize="@dimen/_16ssp" />


                <LinearLayout
                    android:id="@+id/recyclerLayout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_280sdp">


                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerviewobj"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginStart="@dimen/_10sdp"
                        android:layout_marginTop="@dimen/_20sdp"
                        android:layout_marginEnd="@dimen/_10sdp"
                        android:orientation="horizontal"
                        android:nestedScrollingEnabled="false"
                        android:layout_marginBottom="@dimen/_20sdp"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior"
                        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"

                        />
                </LinearLayout>



                <TextView
                    android:id="@+id/notfound"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginStart="@dimen/_15sdp"
                    android:layout_marginTop="@dimen/_20sdp"
                    android:layout_marginBottom="@dimen/_20sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/DISPLAY_LIGHTS"
                    android:gravity="center"
                    android:visibility="gone"
                    android:textColor="?attr/hintTextColors"
                    android:textSize="@dimen/_12ssp" />
                <TextView
                    android:id="@+id/recommendTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/addDeviceLayout"
                    android:layout_below="@+id/recyclerviewobj"
                    android:layout_marginStart="@dimen/_16sdp"
                    android:layout_marginTop="@dimen/_7sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/RECOMMENDATION"
                    android:textColor="@color/gray_scale_placehold"
                    android:textSize="@dimen/_16ssp" />

                <LinearLayout
                    android:id="@+id/addDeviceLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/createRoomLayout"
                    android:layout_marginTop="@dimen/_14sdp"
                    android:background="?attr/buttonTextColor"
                    android:orientation="vertical"
                    tools:visibility="visible">

                    <ImageView
                        android:id="@+id/addBtn"
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:src="@drawable/ic_thermostates_icon"
                        app:tint="?attr/colorPrimaryDark" />

                    <TextView
                        android:id="@+id/addDevice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:fontFamily="@font/lexend_bold"
                        android:text="@string/PROGRAM_DISPLAY_SENSOR_PLUGS"
                        android:textColor="?attr/colorPrimaryDark"
                        android:textSize="@dimen/_12ssp" />

                    <TextView
                        android:id="@+id/summry"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:layout_marginBottom="@dimen/_6sdp"
                        android:fontFamily="@font/lexend_medium"
                        android:text="@string/DISPLAY_SENSOR_SUB_TITLE"
                        android:textColor="?attr/secondaryTextColor"
                        android:textSize="@dimen/_9ssp" />

                    <RelativeLayout
                        android:id="@+id/container3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_6sdp"
                        android:layout_marginTop="@dimen/_6sdp"
                        android:layout_marginBottom="@dimen/_10sdp">

                        <TextView
                            android:id="@+id/get_started"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginStart="@dimen/_10sdp"
                            android:fontFamily="@font/lexend_semibold"
                            android:text="@string/RECOMMENDED_GROUP"
                            android:textAllCaps="true"
                            android:textSize="@dimen/_9ssp" />

                        <ImageView
                            android:id="@+id/forward_arrow"
                            android:layout_width="@dimen/_16sdp"
                            android:layout_height="@dimen/_16sdp"
                            android:layout_alignParentEnd="true"
                            android:layout_marginRight="@dimen/_20sdp"
                            android:src="@drawable/ic_forward_arrow"
                            app:tint="?attr/colorPrimary" />

                    </RelativeLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/createRoomLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_marginTop="@dimen/_9sdp"
                    android:layout_marginBottom="@dimen/_20sdp"
                    android:background="?attr/buttonTextColor"
                    android:orientation="vertical"
                    tools:visibility="visible">

                    <ImageView
                        android:id="@+id/addBtnRoom"
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:src="@drawable/rgb_light_new"
                        app:tint="?attr/colorPrimaryDark" />

                    <TextView
                        android:id="@+id/addRooms"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:fontFamily="@font/lexend_bold"
                        android:text="@string/DISPLAY_RGBW"
                        android:textColor="?attr/colorPrimaryDark"
                        android:textSize="@dimen/_12ssp" />

                    <TextView
                        android:id="@+id/summry1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:layout_marginBottom="@dimen/_6sdp"
                        android:fontFamily="@font/lexend_medium"
                        android:text="@string/PROGRAM_DISPLAY_RGB_MSG"
                        android:textColor="?attr/secondaryTextColor"
                        android:textSize="@dimen/_9ssp" />

                    <RelativeLayout
                        android:id="@+id/container99"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_6sdp"
                        android:layout_marginTop="@dimen/_6sdp"
                        android:layout_marginBottom="@dimen/_10sdp">

                        <TextView
                            android:id="@+id/get_started_1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginStart="@dimen/_10sdp"
                            android:fontFamily="@font/lexend_semibold"
                            android:text="@string/RECOMMENDED_GROUP"
                            android:textAllCaps="true"
                            android:textSize="@dimen/_9ssp" />

                        <ImageView
                            android:id="@+id/forward_arrow1"
                            android:layout_width="@dimen/_16sdp"
                            android:layout_height="@dimen/_16sdp"
                            android:layout_alignParentEnd="true"
                            android:layout_marginRight="@dimen/_20sdp"
                            android:src="@drawable/ic_forward_arrow"
                            app:tint="?attr/colorPrimary" />

                    </RelativeLayout>
                </LinearLayout>




            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
    </LinearLayout>

</layout>

这里使用recyclerview的属性

app:layout_behavior="@string/appbar_scrolling_view_behavior"

并像这样关闭recyclerview嵌套滚动

android:nestedScrollingEnabled="false"

其他回答

您可以使用我的示例代码

    <?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <LinearLayout
        android:id="@+id/fl_all_brand"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".view.fragment.AllBrandFragment">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <LinearLayout
                android:id="@+id/fl_all_brand1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:orientation="vertical">


                <!--<include layout="@layout/content_home" />-->

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginStart="@dimen/_15sdp"
                    android:layout_marginTop="@dimen/_20sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/DISPLAY_LIGHTS"
                    android:textColor="@color/gray_scale_placehold"
                    android:textSize="@dimen/_16ssp" />


                <LinearLayout
                    android:id="@+id/recyclerLayout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_280sdp">


                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerviewobj"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginStart="@dimen/_10sdp"
                        android:layout_marginTop="@dimen/_20sdp"
                        android:layout_marginEnd="@dimen/_10sdp"
                        android:orientation="horizontal"
                        android:nestedScrollingEnabled="false"
                        android:layout_marginBottom="@dimen/_20sdp"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior"
                        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"

                        />
                </LinearLayout>



                <TextView
                    android:id="@+id/notfound"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginStart="@dimen/_15sdp"
                    android:layout_marginTop="@dimen/_20sdp"
                    android:layout_marginBottom="@dimen/_20sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/DISPLAY_LIGHTS"
                    android:gravity="center"
                    android:visibility="gone"
                    android:textColor="?attr/hintTextColors"
                    android:textSize="@dimen/_12ssp" />
                <TextView
                    android:id="@+id/recommendTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/addDeviceLayout"
                    android:layout_below="@+id/recyclerviewobj"
                    android:layout_marginStart="@dimen/_16sdp"
                    android:layout_marginTop="@dimen/_7sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/RECOMMENDATION"
                    android:textColor="@color/gray_scale_placehold"
                    android:textSize="@dimen/_16ssp" />

                <LinearLayout
                    android:id="@+id/addDeviceLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/createRoomLayout"
                    android:layout_marginTop="@dimen/_14sdp"
                    android:background="?attr/buttonTextColor"
                    android:orientation="vertical"
                    tools:visibility="visible">

                    <ImageView
                        android:id="@+id/addBtn"
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:src="@drawable/ic_thermostates_icon"
                        app:tint="?attr/colorPrimaryDark" />

                    <TextView
                        android:id="@+id/addDevice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:fontFamily="@font/lexend_bold"
                        android:text="@string/PROGRAM_DISPLAY_SENSOR_PLUGS"
                        android:textColor="?attr/colorPrimaryDark"
                        android:textSize="@dimen/_12ssp" />

                    <TextView
                        android:id="@+id/summry"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:layout_marginBottom="@dimen/_6sdp"
                        android:fontFamily="@font/lexend_medium"
                        android:text="@string/DISPLAY_SENSOR_SUB_TITLE"
                        android:textColor="?attr/secondaryTextColor"
                        android:textSize="@dimen/_9ssp" />

                    <RelativeLayout
                        android:id="@+id/container3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_6sdp"
                        android:layout_marginTop="@dimen/_6sdp"
                        android:layout_marginBottom="@dimen/_10sdp">

                        <TextView
                            android:id="@+id/get_started"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginStart="@dimen/_10sdp"
                            android:fontFamily="@font/lexend_semibold"
                            android:text="@string/RECOMMENDED_GROUP"
                            android:textAllCaps="true"
                            android:textSize="@dimen/_9ssp" />

                        <ImageView
                            android:id="@+id/forward_arrow"
                            android:layout_width="@dimen/_16sdp"
                            android:layout_height="@dimen/_16sdp"
                            android:layout_alignParentEnd="true"
                            android:layout_marginRight="@dimen/_20sdp"
                            android:src="@drawable/ic_forward_arrow"
                            app:tint="?attr/colorPrimary" />

                    </RelativeLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/createRoomLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_marginTop="@dimen/_9sdp"
                    android:layout_marginBottom="@dimen/_20sdp"
                    android:background="?attr/buttonTextColor"
                    android:orientation="vertical"
                    tools:visibility="visible">

                    <ImageView
                        android:id="@+id/addBtnRoom"
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:src="@drawable/rgb_light_new"
                        app:tint="?attr/colorPrimaryDark" />

                    <TextView
                        android:id="@+id/addRooms"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:fontFamily="@font/lexend_bold"
                        android:text="@string/DISPLAY_RGBW"
                        android:textColor="?attr/colorPrimaryDark"
                        android:textSize="@dimen/_12ssp" />

                    <TextView
                        android:id="@+id/summry1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:layout_marginBottom="@dimen/_6sdp"
                        android:fontFamily="@font/lexend_medium"
                        android:text="@string/PROGRAM_DISPLAY_RGB_MSG"
                        android:textColor="?attr/secondaryTextColor"
                        android:textSize="@dimen/_9ssp" />

                    <RelativeLayout
                        android:id="@+id/container99"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_6sdp"
                        android:layout_marginTop="@dimen/_6sdp"
                        android:layout_marginBottom="@dimen/_10sdp">

                        <TextView
                            android:id="@+id/get_started_1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginStart="@dimen/_10sdp"
                            android:fontFamily="@font/lexend_semibold"
                            android:text="@string/RECOMMENDED_GROUP"
                            android:textAllCaps="true"
                            android:textSize="@dimen/_9ssp" />

                        <ImageView
                            android:id="@+id/forward_arrow1"
                            android:layout_width="@dimen/_16sdp"
                            android:layout_height="@dimen/_16sdp"
                            android:layout_alignParentEnd="true"
                            android:layout_marginRight="@dimen/_20sdp"
                            android:src="@drawable/ic_forward_arrow"
                            app:tint="?attr/colorPrimary" />

                    </RelativeLayout>
                </LinearLayout>




            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
    </LinearLayout>

</layout>

这里使用recyclerview的属性

app:layout_behavior="@string/appbar_scrolling_view_behavior"

并像这样关闭recyclerview嵌套滚动

android:nestedScrollingEnabled="false"

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

就我而言,这对我很有效

将android:fillViewport="true"放入NestedScrollView中。 使RecyclerView的高度为wrap_content,即android:layout_height="wrap_content" 将此添加到RecyclerView android:nestedScrollingEnabled="false"

OR

以编程方式,在Kotlin类中

recyclerView。isNestedScrollingEnabled = false

mRecyclerView.setHasFixedSize(假)

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

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

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

谢谢