我有一个垂直的recyclerview(使用GridLayoutManager
)放在另一个recyclerview(使用LinearLayoutManager
)内。我现在面临的问题是,内部的recyclerview(使用GridLayoutManager)会将所有项目同时绑定,即使当前屏幕上没有这些视图(对于其所有项都调用onBindViewHolder()
)。
为了给您更多信息,在我的布局文件中,我将recyclerview的高度设置为wrap_content
。
我认为问题在于,由于有两个嵌套的垂直recyclerviews,当父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