我没有通过使用 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>
然后我在 OnBindViewHolder() 中将父 ArrayList 拆分为左右 ArrayList。我最终得到了两个子 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);