【发布时间】:2016-07-21 18:55:40
【问题描述】:
我在另一个回收站视图(LinearLayoutManager)中有一个垂直回收站视图(GridLayoutManager)。我现在面临的问题是,内部 recyclerview(使用 GridLayoutManager)同时绑定了它的所有项目,即使是目前不在屏幕上的视图(onBindViewHolder() 被调用它的所有项目)。
为了提供更多信息,在我的布局文件中,我将回收站视图的高度设置为wrap_content。
我认为问题在于,由于有 2 个嵌套的垂直回收视图,当父 RV 想要测量其子级而子级是另一个 RV 时,在onMeasure() 它计算整个 RV 所需的大小,而不仅仅是它想在屏幕上绑定的部分。
知道如何解决这个问题吗?
这是我的外部 recyclerview 的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/component_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
这是我的内部 recyclerview 的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/gutter"
android:paddingBottom="@dimen/gutter">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/gutter"
android:textSize="30sp"
android:textColor="@android:color/white"
android:fontFamily="sans-serif-thin"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
P.S.:我将这个适配器委托用于我的外部 recyclerview: https://github.com/sockeqwe/AdapterDelegates
【问题讨论】:
-
你说得对——回收站视图的长度是无限的,所以子 RV 会认为它适合刚刚找到的每个视图。我不会给出答案,因为我不能 100% 确定,但我认为您需要制作一个自定义布局管理器。
标签: android android-recyclerview gridlayoutmanager nestedrecyclerview