【发布时间】:2015-05-08 21:50:15
【问题描述】:
我正在使用 RecyclerView 和 StaggeredGridLayoutManager 来制作一个两列的列表。但是如何在左列和右列之间设置右边距。我已经使用此代码从顶部制作右边距,但是如何解决列之间的双重空格。
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
// Add top margin only for the first or second item to avoid double space between items
// Add top margin only for the first or second item to avoid double space between items
if((parent.getChildCount() > 0 && parent.getChildPosition(view) == 0)
|| (parent.getChildCount() > 1 && parent.getChildPosition(view) == 1))
outRect.top = space;
}
在活动中:
recyclerView.addItemDecoration(new SpacesItemDecoration(20));
我尝试使用view.getX(),它总是返回0。
谁能帮帮我?非常感谢!
【问题讨论】:
标签: android android-recyclerview margins staggeredgridlayout