0

I have a RecyclerView with a StaggeredGridLayoutManager. The problem is that before a full span element there is a gap that I need to fill, increasing/decreasing the height of the last two elements. A picture of the problem:

enter image description here

  • Just 2 columns are required.
  • The elements of the grid can have different heights.

I'm trying to calculate the height of the two columns using each element height, and at the end see which column is longer, and add height to the shortest, but I don't know where is a safe place to set the new height. When I know for sure the height of the view is the one is going to be draw.

I'm stuck for days with this problem, any suggestions are welcome!

Julio_oa
  • 570
  • 6
  • 15

1 Answers1

0

onBindViewHolder

double positionHeight = getPositionRatio(position);
  holder.img_event).setHeightRatio(positionHeight);
 private double getPositionRatio(final int position) {
        double ratio = sPositionHeightRatios.get(position, 0.0);
        // if not yet done generate and stash the columns height
        // in our real world scenario this will be determined by
        // some match based on the known height and width of the image
        // and maybe a helpful way to get the column height!
        if (ratio == 0) {
            ratio = getRandomHeightRatio();
            sPositionHeightRatios.append(position, ratio);
            Log.d("'", "getPositionRatio:" + position + " ratio:" + ratio);
        }
        return ratio;
    }
private final Random mRandom = new Random();

    private double getRandomHeightRatio() {
        return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5 the width
    }
Barnali Bhattacharjee
  • 497
  • 2
  • 10
  • 24