Android RecyclerView addItemDecoration Won't Add Decoration to Bottom of RecyclerView

ぃ、小莉子 提交于 2019-12-06 13:13:33

As per Janice Kartika's suggestion, I copied and pasted the code into my project. After I copied and pasted Janice's code, Android Studio gave me this suggestion:

Replace with android.support.v7.widge.DividerItemDecoration?

Older versions of the RycyclerView library did not include a divider decorator, but one was provided as a sample in the support demos. This divider class has been widely copy/pasted into various projects.

In recent versions of the support library, the divider decorator is now included, so you can replace custom copies with the "built-in" version, android.support.v7.widget.DividerItemDecoration

So instead of using HorizontalDividerItemDecoration (which was part of an external library that we had imported) like this:

decor = new HorizontalDividerItemDecoration.Builder(context).build();
addItemDecoration(decor, -1);

I used the default "DividerItemDecoration" like this:

decor = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
addItemDecoration(decor);

and it worked. Turns out if you do not put and "int index" into addItemDecoration, it will default to -1 and add the decoration to the bottom by default.

I got the same problem like this long time ago and I was able to solve this by copy-pasting DividerItemDecoration class from this link. But now it seems like some methods are deprecated, so I made a slight modification to it:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };
    private static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    private static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;
    private int mOrientation;

    public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    private void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    private void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}

Copy-paste it to your project, and use it like this:

public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    setHasFixedSize(true);
    setLayoutManager(new LinearLayoutManager(context));
    addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!