简体   繁体   English

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

[英]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.

相关问题 如何防止在Android中的RecyclerView上向左或向右滑动 - How to prevent swipe left or right on a RecyclerView in Android 在Android中从右到左布局 - Right to left layout in android Android Layout 在水平布局中左右对齐 - Android Layout left and right align in horizontal layout 如何在Android中创建具有左上角和右上角圆角的布局? - How to create layout having top Left and Right Round Corners in android? 如何在Android中向左对齐文本布局和向右对齐图像 - How to align a text layout to the left and an image to the right in android Android布局 - 如何使控件在没有填充的情况下左/右显示? - Android layout - How to make the controls appear to the left/right without padding? 如何在Android中将布局调整为适合屏幕而没有左,右角的空间 - how to fit layout to screen without space left and right corners in android 如何在Android的相对布局中将控件左,右和中心对齐 - how to align controls left, right and center in relative layout in android 如何在Android Studio中的布局中实现滑动(向左/向右)手势? - How to implement swipe (left/right) gesture in a layout in Android Studio? 如何在android中的布局上实现左/右滑动/拖动 - How implement left/ right swipe/fling on layout in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM