我在一个布局中有两个RecyclerView
。到目前为止,我无法滚动页面本身,只能滚动每个RecyclerView
,感觉很奇怪。现在,我试图找出滚动两个RecyclerView
的页面的最佳方法。我听说当我将它们放入NestedScrollView
时,它们会停止回收其视图。
RecyclerView
放在NestedScrollView
中是对的,这会禁用RecyclerView
中项目的回收吗? 更新
这是我的布局。假设recycler1
和recycler2
没有携带在一起相关的项目,因此将它们放在一个RecyclerView
中(使用不同的视图类型)对我来说在语义上是错误的。
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="first title"
app:layout_constraintBottom_toTopOf="@+id/recycler1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text1"
tools:itemCount="3"
tools:listitem="@layout/item_category" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:text="second title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/recycler1" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text2"
tools:itemCount="3"
tools:listitem="@layout/item_category" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 0 :(得分:2)
这正是ConcatAdapter
中Concatenate adapters sequentially with ConcatAdapter blog post的用例:
ConcatAdapter
是recyclerview:1.2.0-alpha02
中可用的新类,它使您能够顺序组合多个适配器以在单个RecyclerView
中显示。 这使您能够更好地封装适配器,而不必将许多数据源组合到单个适配器中,从而使它们集中并可重复使用。
这允许您将布局写为单个RecyclerView
,可以正确回收视图,同时将每个适配器(及其数据加载)分开。
在您的情况下,您应该考虑实际上有4个适配器-两个简单的标题适配器(或您可以编写的两个相同的TitleAdapter
实例),以及每个以前的适配器一个。
然后,您要通过传递所有4个适配器以使一个可滚动的ConcatAdapter
来构造RecyclerView
:
val firstTitleAdapter = TitleAdapter("first title")
val firstListAdapter: FirstListAdapter = …
val secondTitleAdapter = TitleAdapter("second title")
val secondListAdapter: SecondListAdapter = …
val concatAdapter = ConcatAdapter(firstTitleAdapter, firstListAdapter,
secondTitleAdapter, secondListAdapter)
recyclerView.adapter = concatAdapter
答案 1 :(得分:0)
NestedScrollView
-确实可以做到。 NestedScrollView
的全部要点在于,它计算了所有回收者的所有子视图的大小。为此,它需要呈现列表的所有子视图-因此,实际上,Android矛盾的是,它无法RecycleView
RecyclerView
中的。对于没有分页的小列表来说,这是可以的,但对于大列表,这是一个主要的性能问题。
该问题的解决方案在于设计(或程序设计)领域。而不是使用两个RecyclerView
,而是使用一个two view types。第二种方法-将屏幕分为两个列表,将屏幕分为两个,每个列表为一个。
通常有两个需要彼此滚动的列表是不正确的,因此请避免使用此类解决方案。
希望有帮助。