【发布时间】:2018-06-02 09:55:52
【问题描述】:
我有两个RecyclerViews 垂直放置在LinearLayout 中。我需要使它们都可以滚动,这就是为什么我将LinearLayout 放入NestedScrollView
这是我的布局文件。
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/featured_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/all_topic_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
另外,我正在禁用 Java 代码中的嵌套滚动。
disableNestedScrolling(findViewById(R.id.all_topic_list));
disableNestedScrolling(findViewById(R.id.featured_list));
我的RecylerView库版本是26.1.0
这工作得很好,但随后onBindViewHolder 方法被调用用于列表中的所有项目。理想情况下,应该只为列表中的可见项目调用它。
我认为问题正在发生,因为我将wrap_content 提供给RecyclerView。 this 问题的很多答案表明该问题已在 v23.2.1 中解决,但我已经在使用 v26.1.0。如何解决这个问题?
【问题讨论】:
-
我想你可以查看answer here。您可以考虑在一个
RecyclerView中包含两组数据,而不是使用两个单独的RecyclerView,以便更好地优化您的视图。 -
数据来自两个不同的api,因此我们需要维护两个不同的recyclerview。另外,第一个是水平的recyclerview,第二个是垂直的,所以我的情况是不可能的。
-
我支持使用单个
RecyclerView的建议。您可以让第一个元素成为另一个水平的RecyclerView。我不确定为什么来自两个 api 的数据是一个问题,只是创建适配器以正确处理不同的视图类型。
标签: android android-recyclerview