有哪些方法可以使视图之间的垂直间距相同,如下图所示?
(我有什么)
(我需要什么)
我尝试过使用LinearLayout的高度来为每个“瓷砖”充气,但似乎无法弄清楚这一点。
答案 0 :(得分:6)
答案 1 :(得分:1)
我们不需要任何外部库,因为Android的原生RecyclerView只需更改RecyclerView的布局管理器即可实现Pinterest砌体布局
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
RecyclerAdapter adapter = new RecyclerAdapter(this);
mRecyclerView.setAdapter(adapter);
冷却。它非常简单,但我的LinearLayout上的保证金似乎不起作用。所以这是一个快速修复。
SpacesItemDecoration decoration = new SpacesItemDecoration(16);
mRecyclerView.addItemDecoration(decoration);
SpacesItemDecoration类:
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private final int mSpace;
public SpacesItemDecoration(int space) {
this.mSpace = space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = mSpace;
outRect.right = mSpace;
outRect.bottom = mSpace;
// Add top margin only for the first item to avoid double space between items
if (parent.getChildAdapterPosition(view) == 0)
outRect.top = mSpace;
}
}