0

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

在此处输入图像描述

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

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

我被这个问题困扰了好几天,欢迎提出任何建议!

4

1 回答 1

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
    }
于 2016-08-18T10:58:31.447 回答