Gab*_*iel 17 android gridlayoutmanager android-recyclerview nestedrecyclerview
我GridLayoutManager在另一个Recyclerview(with LinearLayoutManager)中有一个垂直的Recyclerview(带有a ).我现在面临的问题是,内部的recyclerview(使用GridLayoutManager)同时绑定它的所有项目,甚至是当前不在屏幕上的视图(onBindViewHolder()被调用所有项目).
为了给你提供更多信息,在我的布局文件中,我把我的Recycler视图的高度设置为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>
Run Code Online (Sandbox Code Playgroud)
以下是我的内部回收者视图的代码:
<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>
Run Code Online (Sandbox Code Playgroud)
PS:我正在使用此适配器委托进行外部回收查看:https: //github.com/sockeqwe/AdapterDelegates
我认为嵌套的recyclerviews是一个非常糟糕的主意.当我尝试滚动时,哪个recyclerview必须回应scolling,parrent或child.
这就是为什么我认为你在寻找ExpandableListView?这仅限于两个级别的列表,但听起来它可以满足您的需求).它还解决了解决问题.
它看起来像这样:
编辑:甚至嵌套的ExpandableListViews是可能的:
编辑:检查此 lib以进行水平滚动
您需要知道的第一件事是,当您嵌套滚动布局时,内部布局将获得无限允许的高度,从而有效地使它们成为wrap_content。事实上有一个相对简单的方法可以解决这个问题。假设我有两个嵌套的 RecyclerView,例如这些,在本例中是垂直方向的。
<RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical>
<View .../>
<!-- other stuff -->
<RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</RecyclerView>
Run Code Online (Sandbox Code Playgroud)
这里的内部 recyclerView 每次都会立即绑定它的所有子视图,因为从它的位置来看,你的屏幕将具有无限的高度。
解决方案是将内部recyclerview的高度设置为某个静态值,而不是wrap_content或匹配父级,因为其中任何一个都会简单地用一个视图填充外部recyclerview,由于高度很大,该视图将同时绑定。如果您使内部回收器视图的高度与显示器的高度相同,您应该会看到问题消失。
这是一个不会同时绑定所有子项的实现:
<RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical>
<View .../>
<!-- other stuff -->
<RecyclerView
android:layout_width="match_parent"
android:layout_height="@dimen/screen_height"
android:orientation="vertical"/>
</RecyclerView>
Run Code Online (Sandbox Code Playgroud)
请注意,内部 RecyclerView 的layout_height 现在是从尺寸文件中提取的固定值。您自己必须想出一个合理的值来放在那里。
旁注:为了使所有这些工作并使滚动正常工作,您可能需要使用 RecyclerView 中的参数:NestedScrollingEnabled - 有几个与此相关的已知错误,您可能需要解决这些错误。
即:innerRecyclerView.setHasFixedSize(true);和innerRecyclerView.setNestedScrollingEnabled(false)。
| 归档时间: |
|
| 查看次数: |
2990 次 |
| 最近记录: |