我有一个带有动态元素数量的RecyclerView(事先不知道元素的高度)。如果所有元素的高度都小于屏幕高度,我想在元素之间添加相等的空格。有没有一种简单的方法可以做这样的事情?
谢谢!
CraniumRat
答案 0 :(得分:0)
使用此类动态地向recyclelerview添加空间:
public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {
private final int mVerticalSpaceHeight;
public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) {
this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.bottom = mVerticalSpaceHeight;
}
}
如果您设置列表或所有元素更改的高度总和,那么还要更新mVerticalSpaceHeight。
添加以下行:
rv.addItemDecoration(new VerticalSpaceItemDecoration(PUT THE CALCULATED HEIGHT IN HERE))