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

当前回答

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

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

其他回答

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

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

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

就我而言,这对我很有效

将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:id="@+id/rv_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</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"