RecycleView 简单设置分割线

来源:互联网 发布:编程真好玩 dk出版社 编辑:程序博客网 时间:2024/06/08 15:12

先上效果

效果

直接上代码

  • 使用默认方式
recyclerView.addItemDecoration( new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
  • 使用自定义颜色及高度(通过shape 控制颜色及高度)
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_recycleview_divider));        recyclerView.addItemDecoration(dividerItemDecoration);

R.drawable.shape_recycleview_divider

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">    <solid android:color="#F3F5F7"/>    <size android:height="2dp"/></shape>

DividerItemDecoration源码

/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package android.support.v7.widget;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Rect;import android.graphics.drawable.Drawable;import android.support.annotation.NonNull;import android.support.v4.view.ViewCompat;import android.view.View;import android.widget.LinearLayout;/** * DividerItemDecoration is a {@link RecyclerView.ItemDecoration} that can be used as a divider * between items of a {@link LinearLayoutManager}. It supports both {@link #HORIZONTAL} and * {@link #VERTICAL} orientations. * * <pre> *     mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), *             mLayoutManager.getOrientation()); *     recyclerView.addItemDecoration(mDividerItemDecoration); * </pre> */public class DividerItemDecoration extends RecyclerView.ItemDecoration {    public static final int HORIZONTAL = LinearLayout.HORIZONTAL;    public static final int VERTICAL = LinearLayout.VERTICAL;    private static final int[] ATTRS = new int[]{ android.R.attr.listDivider };    private Drawable mDivider;    /**     * Current orientation. Either {@link #HORIZONTAL} or {@link #VERTICAL}.     */    private int mOrientation;    private final Rect mBounds = new Rect();    /**     * Creates a divider {@link RecyclerView.ItemDecoration} that can be used with a     * {@link LinearLayoutManager}.     *     * @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);        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 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) {        if (drawable == null) {            throw new IllegalArgumentException("Drawable cannot be null.");        }        mDivider = drawable;    }    @Override    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {        if (parent.getLayoutManager() == null) {            return;        }        if (mOrientation == VERTICAL) {            drawVertical(c, parent);        } else {            drawHorizontal(c, parent);        }    }    @SuppressLint("NewApi")    private void drawVertical(Canvas canvas, RecyclerView parent) {        canvas.save();        final int left;        final int right;        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 = parent.getChildCount();        for (int i = 0; i < childCount; i++) {            final View child = parent.getChildAt(i);            parent.getDecoratedBoundsWithMargins(child, mBounds);            final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));            final int top = bottom - mDivider.getIntrinsicHeight();            mDivider.setBounds(left, top, right, bottom);            mDivider.draw(canvas);        }        canvas.restore();    }    @SuppressLint("NewApi")    private void drawHorizontal(Canvas canvas, RecyclerView parent) {        canvas.save();        final int top;        final int bottom;        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 = parent.getChildCount();        for (int i = 0; i < childCount; i++) {            final View child = parent.getChildAt(i);            parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);            final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child));            final int left = right - mDivider.getIntrinsicWidth();            mDivider.setBounds(left, top, right, bottom);            mDivider.draw(canvas);        }        canvas.restore();    }    @Override    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,            RecyclerView.State state) {        if (mOrientation == VERTICAL) {            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());        } else {            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);        }    }}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 西凤御窖酒52度浓香型 泸州御酒52度浓香型 沧州御河老酒39度价格 御阵法不 御泥坊 御泥坊面膜 御泥坊面膜怎么样 御泥坊产品 御泥坊产品怎么样 御泥坊怎么样 御泥坊护肤品 御泥坊吧 御泥坊那款面膜好 御泥坊洁面乳 御泥坊美白系列 御泥坊的祛痘面膜 御泥坊好用吗 御泥坊旗舰店 御泥坊app 御泥坊化妆品 化妆品御泥坊 御泥坊多少钱一套 御泥坊产品好不好 御泥坊代理微商 御泥坊网店代理 御泥坊御泥坊 御泥坊公司 御泥坊套装价格 御泥坊正品 正品御泥坊 御泥坊面膜的价格 御泥坊一套多少钱 男士面膜御泥坊 御泥坊去角质 御泥坊美白面膜泥 淘宝御泥坊旗舰店 御泥坊 男士 御泥坊优惠券 御泥坊洋甘菊 御泥坊淘宝 御泥坊代言人