StaggeredGridLayoutManager在完整span元素之前留下了间隙

时间:2016-08-08 19:28:17

标签: android staggered-gridview

我有一个带有StaggeredGridLayoutManager的RecyclerView。 问题是在全跨度元素之前,我需要填充一个间隙,增加/减少最后两个元素的高度。 问题图片:

enter image description here

  • 只需要2列。
  • 网格的元素可以有不同的高度。

我尝试使用每个元素高度来计算两列的高度,并在最后查看哪个列更长,并将高度添加到最短,但我不知道哪里是安全的地方设置新的高度。当我确切地知道视图的高度是将要绘制的时候。

我已经坚持了几天这个问题,欢迎任何建议!

1 个答案:

答案 0 :(得分: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
    }