[英]How to control left and right handed layout placement in an Android RecyclerView
I have a left handed and right handed layout which need to be placed in the appropriate column in a RecyclerView.我有一个左手和右手布局,需要放置在 RecyclerView 的相应列中。 I'm using a StaggeredGridLayout as the layouts can be of different sizes as well.
我使用的是 StaggeredGridLayout,因为布局也可以有不同的大小。 I'm trying to control the placement of the child layouts and not having much luck as once the left/right orientation breaks it stays broken.
我正在尝试控制子布局的位置,并且运气不佳,因为一旦左/右方向中断,它就会保持中断。
How the layout is breaking布局是如何破坏的
The GridLayoutManager was better at keeping the layouts in order but created odd spacing when the larger layouts were used. GridLayoutManager 更擅长保持布局有序,但在使用较大布局时会产生奇怪的间距。 Even if I could use the GridLayoutManager I would still need the ability to control the left/right sides as the data can arrive in a variety of orders.
即使我可以使用 GridLayoutManager,我仍然需要能够控制左侧/右侧,因为数据可以以各种顺序到达。
I should also note that this RecyclerView is already inside of another RecyclerView so the adapter and layoutManager are being set inside of the onBindViewHolder() of the parent RV.我还应该注意,这个 RecyclerView 已经在另一个 RecyclerView 内,因此适配器和 layoutManager 被设置在父 RV 的 onBindViewHolder() 内。
Instead of solving this by using the GridLayoutManager options I added GridLayout in the xml.我没有通过使用 GridLayoutManager 选项来解决这个问题,而是在 xml 中添加了 GridLayout。
<GridLayout
android:id="@+id/grid_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:columnCount="2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text_panel">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/left_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/grid_container" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/right_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/grid_container" />
</GridLayout>
Then I split the parent ArrayList into a right and left ArrayList in the OnBindViewHolder().然后我在 OnBindViewHolder() 中将父 ArrayList 拆分为左右 ArrayList。 I end up with two child RecyclerViews, but it works and I have fine control over the appearance.
我最终得到了两个子 RecyclerViews,但它可以工作并且我可以很好地控制外观。
ArrayList<Breaker> leftList = new ArrayList<>();
ArrayList<Breaker> rightList = new ArrayList<>();
for(int i = 0; i < p.getCount(); i++) {
if (p.getB(i).getType() % 2 == 0) {
leftList.add(p.getB(i));
} else {
rightList.add(p.getB(i));
}
}
LinearLayoutManager leftLayout = new LinearLayoutManager(h.leftBView.getContext());
h.leftBView.setLayoutManager(leftLayout);
MultiAdapter leftBAdapter = new MultiBAdapter(leftList,
h.leftBView.getContext());
h.leftBView.setAdapter(leftBAdapter);
LinearLayoutManager rightLayout = new LinearLayoutManager(h.rightBView.getContext());
h.rightBView.setLayoutManager(rightLayout);
MultiBAdapter rightBAdapter = new MultiBAdapter(rightList,
h.rightBView.getContext());
h.rightBView.setAdapter(rightBAdapter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.