19

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 を返します。

誰でも私を助けることができますか?どうもありがとう!

4

9 に答える 9

10

次のコードは、StaggeredGridLayoutManager、GridLayoutManager、および LinearLayoutManager を処理します。

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

    private int halfSpace;

    public SpacesItemDecoration(int space) {
        this.halfSpace = space / 2;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        if (parent.getPaddingLeft() != halfSpace) {
            parent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace);
            parent.setClipToPadding(false);
        }

        outRect.top = halfSpace;
        outRect.bottom = halfSpace;
        outRect.left = halfSpace;
        outRect.right = halfSpace;
    }
}
于 2015-08-24T19:35:31.697 に答える
3

item margin と RecyclerView padding を設定して自分で解決しました。マージンとパディングの両方が予想されるスペースの半分であるため、問題は解消されました。

于 2015-03-08T07:42:46.120 に答える
0

相対的な質問の両方の回答を少し変更して修正します

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private final int mSpace;

    public SpacesItemDecoration(int space) {
        this.mSpace = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        int spanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
        if (spanIndex == 0) {
            outRect.left = 30;
            outRect.right = 15;
        } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span
            outRect.left = 15;
            outRect.right = 30;
        }
        outRect.bottom = 30;
        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildAdapterPosition(view) == 0) {
            outRect.top = 30;
            outRect.right = 30;
        }
    }
}
于 2016-01-19T09:41:59.137 に答える
0

RecyclerView の両側にパディングを設定することができます。

myRecyclerView.setPadding(10,0,10,0);

このようにして、側面と列の間のスペースを均等にすることができます。

于 2018-07-06T17:22:08.047 に答える
0

spanindex に従って ItemDecoration に異なる左右のスペースを設定することは、アイテム ビューのサイズが固定されている場合にのみうまく機能します。アイテム ビューに高さで wrap_content を設定したイメージ ビューが含まれている場合、アイテム ビューはスパン位置を変更する可能性がありますが、スパン インデックスが希望どおりに更新されず、余白がめちゃくちゃになります。

私のやり方は、ビューアイテムに左右対称のスペースを使用することです。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // init
    recyclerView = (RecyclerView) rv.findViewById(R.id.img_waterfall);
    layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(new MyAdapter(getContext()));
    recyclerView.addItemDecoration(new SpacesItemDecoration(
        getResources().getDimensionPixelSize(R.dimen.space)));
}


private static class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private final int space;
    public SpacesItemDecoration(int space) {
        this.space = space;
    }
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {
        outRect.bottom = 2 * space;
        int pos = parent.getChildAdapterPosition(view);
        outRect.left = space;
        outRect.right = space;
        if (pos < 2)
            outRect.top = 2 * space;
    }
}

次に、 RecylerView(left/right) android:paddingLeft="@dimen/space" android:paddingRight="@dimen/space" に同じパディングを設定します

    <android.support.v7.widget.RecyclerView
        android:id="@+id/img_waterfall"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/space"
        android:paddingRight="@dimen/space"/>
于 2016-05-05T11:59:26.123 に答える