0

I have a left handed and right handed layout which need to be placed in the appropriate column in a RecyclerView. I'm using a StaggeredGridLayout as the layouts can be of different sizes as well. 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. 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.

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.

JohnK
  • 1
  • 2

1 Answers1

0

Instead of solving this by using the GridLayoutManager options I added GridLayout in the xml.

        <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(). I end up with two child RecyclerViews, but it works and I have fine control over the appearance.

        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);
JohnK
  • 1
  • 2