Android RecyclerView作为NestedScrollView的子级-不循环视图
我正在尝试创建一个可滚动区域,该区域将包含以下类型的各个部分:Android RecyclerView作为NestedScrollView的子级-不循环视图,android,android-recyclerview,android-nestedscrollview,Android,Android Recyclerview,Android Nestedscrollview,我正在尝试创建一个可滚动区域,该区域将包含以下类型的各个部分: 水平回收区 垂直回收区 文本部分 我采用的方法是使用NestedRecyclerView作为所有子部分的父滚动视图。这种观点看起来是这样的: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.
- 水平回收区
- 垂直回收区
- 文本部分
NestedRecyclerView
作为所有子部分的父滚动视图。这种观点看起来是这样的:
<?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">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/mynav_appbarLayout"
android:background="?attr/themeToolbarBg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/mynav_toolbar"
layout="@layout/actionbar_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
<?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">
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/fooBarsRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:nestedScrollingEnabled="false"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
</LinearLayout>
</layout>
现在,当我将这些视图添加到父级NestedScrollView
的LinearLayout
中,并为关联的RecyclerView
设置LayoutManager
到orientationHorizontal
时,效果很好,但是,当我使用orientationVertical
(与NestedScrollView
的方向相同)RecyclerView
不是循环视图。显然,这会导致不可接受的性能
在做了大约一天的研究后,我的头撞到了墙上,似乎在NestedScrollView
中嵌套了一个与NestedScrollView
方向相同的RecyclerView
会导致RecyclerView
失去其回收功能
正如您希望从上面的布局中看到的,我已经尝试了所有我能找到的建议,确保RecyclerView
的高度不是wrap\u content
,使用layout\u behavior
,设置NestedRecyclerView
填充视图端口等等
关于这个问题,我已经用了6页的谷歌搜索,并尝试了在SO或博客上找到的所有建议,但都没有效果
奇怪的是,如果我将NestedScrollView
替换为ScrollView
,垂直RecyclerView
将恢复其回收器功能,但现在独立于父ScrollView
进行滚动,这不符合我们的要求
这是一个已解决的问题,还是我需要重新思考我的整个解决方案?即,我只是缺少了一个属性或在XML中做了一些错误的事情,还是在具有相同方向的嵌套滚动视图中使用RecyclerView
从根本上说是一个问题
以下是资源清单,我对这些建议进行了详尽的尝试,但毫无结果:
根据评论中的建议,我可以使用多类型适配器对此进行建模,这是我以前做过的,但对于这个特定问题,我不确定这种方法是否有效
我认为评论建议我这样做:
适配器将适应以下类型:
- 水平剖面
- 文本部分
- 卡片组
但是,要求是:
因此,正如您所希望看到的,RecyclerView
将有一个方向垂直的LinearLayoutManager
,但是,一旦我们打牌,它们必须以网格方式排列,这当然是LinearLayoutManager
不支持的。因此,也许我可以让最后一部分成为另一个RecyclerView使用GridLayoutManager
?但是,我昨晚尝试了这个,但没有成功,出现了滚动问题,因为最底层的RecyclerView
在最外层的RecyclerView
中垂直滚动,而最外层的RecyclerView也在垂直滚动。您是否在RecyclerView的其他地方引用过它们?如果您需要的话这样垃圾收集器就不会删除它们。不,回收器视图设置正确。正如我所说,如果我将NestedScrollView替换为ScrollView,回收器工作正常,这与在NestedScrollView中使用RecyclerView有关。您发现,相同方向的嵌套滚动视图中的回收器视图将失去其回收能力。处理您的案例的更好方法是只使用一个回收器视图和一个处理多个项目视图类型的多类型适配器。例如,您的项目视图类型有:水平回收器视图类型、与原始适配器类似的普通视图类型,以及最后一个文本节类型。@ThomasCook对于您的问题,您可以使用跨度计数为3的GridLayoutManager。3表示网格布局中可以存在的最大列数。然后在layoutManager上,通过SetPansizeLookup()附加一个跨度尺寸视图。通过这种方式,您可以按项目位置管理回收器视图的跨度计数。更好的方法是首先通过adapter.getItemViewType(位置)获取项目类型,然后按类型返回跨度计数。是的,现在开始!哈