【问题标题】:How to control left and right handed layout placement in an Android RecyclerView如何在 Android RecyclerView 中控制左右手布局放置
【发布时间】:2021-06-09 18:37:41
【问题描述】:

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

How the layout is breaking

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

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

【问题讨论】:

  • 请发布一些代码,以便我们更好地了解您要做什么。

标签: android android-recyclerview gridlayoutmanager staggeredgridlayout


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2012-05-08
    相关资源
    最近更新 更多