パルカワ2

PerlとRubyとイチャラブ

RecyclerView使ってみてる

便利
Master of Recycler View見れば大体の事書いてそう。
GridLayoutManager使ってて、真ん中の幅整えたいとかは RecyclerView.ItemDecoration で出来そうな感じがした。

class CategoryProductDecoration extends RecyclerView.ItemDecoration {
    private int mHorizontal;
    private int mVertical;

    public CategoryProductDecoration(int horizontal, int vertical) {
        mHorizontal = horizontal;
        mVertical = vertical;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();

        int position = layoutParams.getViewLayoutPosition();
        if (position == RecyclerView.NO_POSITION) {
            outRect.set(0, 0, 0, 0);
            return;
        }

        int spanIndex = layoutParams.getSpanIndex();
        outRect.left = spanIndex == 0 ? 0 : mHorizontal; // 一番左は設定しない
        outRect.top = spanIndex == position ? 0 : mVertical; // 一行目は設定しない
        outRect.right = 0;
        outRect.bottom = 0;
    }
}

techbooster.booth.pm