自定义RecyclerView 分隔线

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    /**
     * 横向
     */
    public static final int HORIZONTAL = LinearLayout.HORIZONTAL;

    /**
     * 纵向
     */
    public static final int VERTICAL = LinearLayout.VERTICAL;

    /**
     * 纵向
     */
    private static final int DIVIDER_TYPE_TAG = R.id.TAG_ITEM_DIVIDER_KEY;

    private static final String TAG = "DividerItem";

    private static final int[] ATTRS = new int[] {android.R.attr.listDivider};

    private final Rect mBounds = new Rect();

    private Drawable mDivider;

    private DividerListener mDividerListener;

    /**
     * Current orientation. Either {@link #HORIZONTAL} or {@link #VERTICAL}.
     */
    private int mOrientation;

    private boolean isFinalDividerItemShow = true;

    /**
     * Creates a divider {@link RecyclerView.ItemDecoration} that can be used with a {@link}.
     *
     * @param context Current context, it will be used to access resources.
     * @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}.
     */
    public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        if (mDivider == null) {
            Logger.w(TAG, "divider getDrawable is null");
        }
        a.recycle();
        setOrientation(orientation);
    }

    /**
     * Sets the orientation for this divider. This should be called if {@link RecyclerView.LayoutManager} changes
     * orientation.
     *
     * @param orientation {@link #HORIZONTAL} or {@link #VERTICAL}
     */
    public final void setOrientation(int orientation) {
        if (orientation != HORIZONTAL && orientation != VERTICAL) {
            throw new IllegalArgumentException("INVALID orientation. It should be either HORIZONTAL or VERTICAL");
        }
        mOrientation = orientation;
    }

    /**
     * Sets the {@link Drawable} for this divider.
     *
     * @param drawable Drawable that should be used as a divider.
     */
    public void setDrawable(@NonNull Drawable drawable) {
        mDivider = drawable;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() == null || mDivider == null) {
            return;
        }
        if (mOrientation == VERTICAL) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        canvas.save();
        final int left;
        final int right;
        // noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft();
            right = parent.getWidth() - parent.getPaddingRight();
            canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = 0;
            right = parent.getWidth();
        }

        final int childCount = isFinalDividerItemShow ? parent.getChildCount() : parent.getChildCount() - 1;
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final Drawable divider = getDivider(child);
            if (divider != null) {
                parent.getDecoratedBoundsWithMargins(child, mBounds);
                final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
                final int top = bottom - divider.getIntrinsicHeight();
                divider.setBounds(left, top, right, bottom);
                divider.draw(canvas);
            }
        }
        canvas.restore();
    }

    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        canvas.save();
        final int top;
        final int bottom;
        // noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            top = parent.getPaddingTop();
            bottom = parent.getHeight() - parent.getPaddingBottom();
            canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
        } else {
            top = 0;
            bottom = parent.getHeight();
        }

        final int childCount = isFinalDividerItemShow ? parent.getChildCount() : parent.getChildCount() - 1;
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final Drawable divider = getDivider(child);
            RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
            if (divider != null && layoutManager != null) {
                layoutManager.getDecoratedBoundsWithMargins(child, mBounds);
                final int right = mBounds.right + Math.round(child.getTranslationX());
                final int left = right - divider.getIntrinsicWidth();
                divider.setBounds(left, top, right, bottom);
                divider.draw(canvas);
            }
        }
        canvas.restore();
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        final Drawable divider = getDivider(view);
        if (divider == null) {
            outRect.set(0, 0, 0, 0);
            return;
        }
        if (mOrientation == VERTICAL) {
            outRect.set(0, 0, 0, divider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, divider.getIntrinsicWidth(), 0);
        }
    }

    private Drawable getDivider(View childView) {
        Object typeTag = childView.getTag(DIVIDER_TYPE_TAG);
        if (mDividerListener != null) {
            return mDividerListener.getDividerDrawable(typeTag);
        }
        return mDivider;
    }

    /**
     * 判断是否需要显示最后Item的下划线
     *
     * @return boolean boolean
     */
    public boolean isFinalDividerItemShow() {
        return isFinalDividerItemShow;
    }

    /**
     * 设置是否需要显示最后Item的下划线
     *
     * @param finalDividerItemShow finalDividerItemShow
     */
    public void setFinalDividerItemShow(boolean finalDividerItemShow) {
        isFinalDividerItemShow = finalDividerItemShow;
    }

    /**
     * 设置下滑线监听回调
     *
     * @param dividerListener dividerListener
     */
    public void setDividerListener(DividerListener dividerListener) {
        this.mDividerListener = dividerListener;
    }

    /**
     * [DividerListener]
* * @author zWX607328 * @version [V9.0.1.1, 2019/1/3] * @since V9.0.1.1 */ public interface DividerListener { /** * 获取divider * * @param typeTag divider类型 * @return Drawable 返回的divider */ Drawable getDividerDrawable(Object typeTag); } }

你可能感兴趣的:(工具)