Should have recorded, has been too lazy, do not want to record, but more and more found easy to forget, forget to find it is not good to find, or quickly record two

Waterfall flow left and right division line equal Settings!

I have seen a lot of examples on the Internet, all of which are universal dividers and so on, but the real use of waterfall flow dividers will appear inconsistent.

The reason:

Here is the layout of items for non-waterfall streams:

The layout above is equivalent to a grid layout, where each item is the same height, so

Its index is left, right, left, and position is the exact itemView,

Left 0, right 1, left 2, right 3, and so on…

In this case we can use the following code to successfully set the splitter line.

        /** * The first column sets the left margin to space and the right margin to space/2 (the second column vice-versa) */

        if (parent.getChildLayoutPosition(view) % 2= =0) {

            outRect.left = space;

            outRect.right = space / 2;

        } else {

            outRect.left = space / 2;

            outRect.right = space;

        }

Copy the code

The important thing is, if it’s a waterfall flow, it looks like this:

The position of the waterfall stream loads where the next position is based on which column height is smaller,

You can see in the waterfall flow diagram above, 0 left, 1 right and then 2 to the right, because

The height of the second column is less than the height of the first column, so it’s loaded into the second column, and so on,

The height below is kind of random, and nobody knows what to do about the dividing line,

So using the code above is not going to work,

Drew, we can through StaggeredGridLayoutManager. LayoutParams

GetSpanIndex () in this method, regardless of your height,

It starts right, left, and right, as shown in the following code.


StaggeredGridLayoutManager.LayoutParams params =  (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();

        /** * Set the left margin to space and the right margin to space/2 (the second column vice versa) */

        if (params.getSpanIndex() % 2= =0) {

            outRect.left = space;

            outRect.right = space / 2;

        } else {

            outRect.left = space / 2;

            outRect.right = space;

        }

Copy the code

Reference: Recyclerview of waterfall flow separation line left and right spacing equal problem