繁体   English   中英

如何在 Android RecyclerView 中控制左右手布局放置

[英]How to control left and right handed layout placement in an Android RecyclerView

我有一个左手和右手布局,需要放置在 RecyclerView 的相应列中。 我使用的是 StaggeredGridLayout,因为布局也可以有不同的大小。 我正在尝试控制子布局的位置,并且运气不佳,因为一旦左/右方向中断,它就会保持中断。

布局是如何破坏的

GridLayoutManager 更擅长保持布局有序,但在使用较大布局时会产生奇怪的间距。 即使我可以使用 GridLayoutManager,我仍然需要能够控制左侧/右侧,因为数据可以以各种顺序到达。

我还应该注意,这个 RecyclerView 已经在另一个 RecyclerView 内,因此适配器和 layoutManager 被设置在父 RV 的 onBindViewHolder() 内。

我没有通过使用 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM