RecyclerView - 添加不同的分割线

如何给RecyclerView 添加不同的分割线:

UI出的效果图:

《RecyclerView - 添加不同的分割线》 RecyclerView添加不同分割线.png

关键点:

  • 自定义ItemDecoration

  • 关键的方法:

    RecyclerViewgetChildAdapterPosition(View child)方法:

返回这个itemView 对应的 adapter position
这个adapter position 是适配器里的数据position 拿到了这个position,就能知道viewType 了

  /**
     * Return the adapter position that the given child view corresponds to.
     *
     * @param child Child View to query
     * @return Adapter position corresponding to the given view or {@link #NO_POSITION}
     */
    public int getChildAdapterPosition(View child) {
        final ViewHolder holder = getChildViewHolderInt(child);
        return holder != null ? holder.getAdapterPosition() : NO_POSITION;
    }

code

public class DifWidthDecoration extends RecyclerView.ItemDecoration {

    private Context mContext;
    private Drawable mDividerWidth;
    private Drawable mDividerNarrow;

    private int mOrientation;

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    public DifWidthDecoration(Context context, int oritation) {
        this.mContext = context;
        this.mOrientation = oritation;
        this.mDividerWidth = context.getResources().getDrawable(R.drawable.divider_wider);
        this.mDividerNarrow = context.getResources().getDrawable(R.drawable.divider_narrow);
        setOrientation(oritation);
    }

    //设置屏幕方向
    public void setOrientation(int orientation) {

        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        this.mOrientation = orientation;
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mOrientation == HORIZONTAL_LIST) {
            //横向 list 画竖线
            drawVerticalLine(c, parent, state);
        } else if (mOrientation == VERTICAL_LIST) {
            //竖向list 画横线
            drawHorizontalLine(c, parent, state);
        }

    }

    private static final String TAG = "DifWidthDecoration";

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

        int blankWidth = DisplayUtil.getScreenWidth(mContext) / 10;
        Log.d(TAG, String.format("childCount -> %d", childCount));
        //由于RecyclerViwe 复用ItemView 这里的childCount 是用户可见的 count
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            //接上面:所以要拿到itemView 对应的adapter position,但是方法的参数里不像getItemOffsets()里回调了View view
            //所以 自己取到child 通过RecyclerView 的 getChildAdapterPosition(child) 来取到adapterPosition
            //旧的写法 parent.getAdapter().getItemType(i) -> 会造成 分割线的错乱
            int adapterPosition = parent.getChildAdapterPosition(child);
            int type = parent.getAdapter().getItemViewType(adapterPosition);

            if (type == DifferentDividerAdapter.item_type_group) {
                //拿到child 的布局信息
                final RecyclerView.LayoutParams params =
                        (RecyclerView.LayoutParams) child.getLayoutParams();
                final int top = child.getBottom() + params.bottomMargin;
                final int bottom = top + mDividerWidth.getIntrinsicHeight();
                mDividerWidth.setBounds(left, top, right, bottom);
                mDividerWidth.draw(c);

            } else if (type == DifferentDividerAdapter.item_type_child) {
                //拿到child 的布局信息
                final RecyclerView.LayoutParams params =
                        (RecyclerView.LayoutParams) child.getLayoutParams();
                final int top = child.getBottom() + params.bottomMargin;
                final int bottom = top + mDividerNarrow.getIntrinsicHeight();
                mDividerNarrow.setBounds(left + blankWidth, top, right, bottom);
                mDividerNarrow.draw(c);
            }
        }
    }

    private void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int top = parent.getPaddingTop();
        int bottom = parent.getHeight() - parent.getPaddingBottom();
        final int childCount = parent.getChildCount();

        int blankWidth = DisplayUtil.getScreenWidth(mContext) / 10;

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);

            int adapterPosition = parent.getChildAdapterPosition(child);
            int type = parent.getAdapter().getItemViewType(adapterPosition);

            if (type == DifferentDividerAdapter.item_type_group) {
                //拿到child 的布局信息
                final RecyclerView.LayoutParams params =
                        (RecyclerView.LayoutParams) child.getLayoutParams();
                final int left = child.getRight() + params.rightMargin;
                final int right = left + mDividerWidth.getIntrinsicWidth();
                mDividerWidth.setBounds(left, top, right, bottom);
                mDividerWidth.draw(c);
            } else if (type == DifferentDividerAdapter.item_type_child) {
                //拿到child 的布局信息
                final RecyclerView.LayoutParams params =
                        (RecyclerView.LayoutParams) child.getLayoutParams();
                final int left = child.getRight() + params.rightMargin;
                final int right = left + mDividerNarrow.getIntrinsicWidth();
                mDividerNarrow.setBounds(left, top + blankWidth, right, bottom);
                mDividerNarrow.draw(c);
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int adapterPosition = parent.getChildAdapterPosition(view);
        int type = parent.getAdapter().getItemViewType(adapterPosition);


        if (mOrientation == HORIZONTAL_LIST) {
            if (type == DifferentDividerAdapter.item_type_group) {
                //画竖线 向右偏移divider 的宽度
                outRect.set(0, 0, mDividerWidth.getIntrinsicWidth(), 0);
            } else if (type == DifferentDividerAdapter.item_type_child) {
                outRect.set(0, 0, mDividerWidth.getIntrinsicWidth(), 0);

            }
        } else if (mOrientation == VERTICAL_LIST) {
            if (type == DifferentDividerAdapter.item_type_group) {
                //画横线 向下偏移divider 的高度
                outRect.set(0, 0, 0, mDividerWidth.getIntrinsicHeight());

            } else if (type == DifferentDividerAdapter.item_type_child) {
                outRect.set(0, 0, 0, mDividerNarrow.getIntrinsicHeight());
            }

        }
    }
}


效果:

《RecyclerView - 添加不同的分割线》 最终效果.png

注意:

onDrawOver() -> drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state)这里:

 final int childCount = parent.getChildCount();

由于RecyclerViwe 复用ItemView 这里的childCount 是用户可见的 count

所以最开始我这样取得itemType :

int type = parent.getAdapter().getItemVieType(i);

直接用遍历时的 i 作为了view 对应的adapter position,
是错误的,随着RecyclerView 的滑动,会造成Divider的错乱(具体表现为:向下移动,好奇的可以自试一下)
因为这个 i 是关于 childCount 的,而childCount 最多就是 屏幕上能展示出的itemView的个数

    原文作者:ZHDelete
    原文地址: https://www.jianshu.com/p/f66fc5ee0615
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞