滚动视图内的Android Recycler视图

问题描述

我有

主视图

<ScrollView
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_products"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:nestedScrollingEnabled="false"
                    />
</ScrollView>

在我的模板适配器上,我还有另一个用于子视图的回收站适配器

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

        <TextView
            android:id="@+id/title_product"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".2"
            android:background="@color/GRAY"
            android:text="TextView" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_for_specification_items"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            app:layoutManager="androidx.recyclerview.widget.linearlayoutmanager"
            android:nestedScrollingEnabled="false"
            />
    </LinearLayout>
</androidx.cardview.widget.CardView>

代码有效,但是当用户在android设备上滚动时,父scrollView不允许我滚动浏览回收站视图(1)

解决方法

使用NestedScrollview而不是scrollview,并且对android:nestedScrollingEnabled="false"使用re <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_products" android:layout_width="match_parent" android:layout_height="wrap_content" android:nestedScrollingEnabled="false" /> </androidx.core.widget.NestedScrollView>

undistort()
,

如果您无法滚动,则是因为此行

android:nestedScrollingEnabled="false"

将其更改为True,因此可以启用滚动:

android:nestedScrollingEnabled="true"

,

在您的代码中,滚动视图中有recylerview,这是错误的。

选项1:如果滚动视图除删除该子视图之外没有其他任何子视图,则只需使用recyclerview并删除android:nestedScrollingEnabled="false

您的主视图代码如下:

<androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_products"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />

选项2:如果滚动视图还有其他子元素(包括recyclerview),则应使用nestedscrollview,如下代码:

<androidx.core.widget.NestedScrollView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scrollbars="none">
      <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:focusableInTouchMode="true"
         android:orientation="vertical">
   <ImageView
      android:id="@+id/sellerProduct"
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:adjustViewBounds="true"
      android:src="@drawable/iphone"
      android:scaleType="fitXY"
      android:contentDescription="@string/app_name" />
      <androidx.recyclerview.widget.RecyclerView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:scrollbars="vertical"
         android:nestedScrollingEnabled="false
         android:id="@+id/productList"/>
      </LinearLayout>
   </androidx.core.widget.NestedScrollView>