RecyclerView を StaggeredGridLayoutManager と共に使用して、2 列のリストを作成しています。しかし、左の列と右の列の間に右マージンを設定する方法。このコードを使用して上から右マージンを作成しましたが、列間の二重スペースを解決する方法.
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 を返します。
誰でも私を助けることができますか?どうもありがとう!