zoukankan      html  css  js  c++  java
  • Recyclerview设置间距

    首先自定义一个RecyclerViewDivider 继承 RecyclerView.ItemDecoration,实现自定义。

    public class RecyclerViewDivider extends RecyclerView.ItemDecoration {
    private Paint mPaint;
    private Drawable mDivider;
    private int mDividerSize = 2;//分割线高度,默认为1px
    private int mOffsetStart = 0;
    private int mOffsetEnd = 0;
    private int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    /**
    * 默认分割线:高度为2px,颜色为灰色
    *
    * @param context
    * @param orientation 列表方向
    */
    private RecyclerViewDivider(Context context, int orientation) {
    if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
    throw new IllegalArgumentException("请输入正确的参数!");
    }
    mOrientation = orientation;

    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();
    }

    /**
    * 自定义分割线
    *
    * @param context
    * @param orientation 列表方向
    * @param drawableId 分割线图片
    */
    public RecyclerViewDivider(Context context, int orientation, int drawableId) {
    this(context, orientation);
    mDivider = ContextCompat.getDrawable(context, drawableId);
    mDividerSize = mDivider.getIntrinsicHeight();
    }

    /**
    * 自定义分割线
    *
    * @param context
    * @param orientation 列表方向
    * @param dividerSize 分割线高度
    * @param dividerColor 分割线颜色
    */
    public RecyclerViewDivider(Context context, int orientation, int dividerSize, @ColorRes int dividerColor) {
    this(context, orientation);
    mDividerSize = dividerSize;
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    mPaint.setColor(context.getResources().getColor(dividerColor));
    mPaint.setStyle(Paint.Style.FILL);
    }


    /**
    * 自定义分割线
    *
    * @param context context
    * @param orientation 列表方向
    * @param dividerHeight 分割线高度
    * @param dividerColor 分割线颜色
    * @param offsetStart 起始偏移量
    * @param offsetEnd 结束偏移量
    */
    public RecyclerViewDivider(Context context, int orientation, int dividerHeight, @ColorRes int dividerColor, int offsetStart, int offsetEnd) {
    this(context, orientation);
    mDividerSize = dividerHeight;
    mOffsetStart = Util.dp2px(context, offsetStart);
    mOffsetEnd = Util.dp2px(context, offsetEnd);
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    if (dividerColor == 0) {
    dividerColor = R.color.lightGray;
    }
    mPaint.setColor(context.getResources().getColor(dividerColor));
    mPaint.setStyle(Paint.Style.FILL);
    }

    //重写此方法,防止设置的波纹背景把分隔线覆盖掉。
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    RecyclerView.ViewHolder childViewHolder = parent.getChildViewHolder(view);
    if (childViewHolder.getItemViewType() != 0) {
    outRect.set(0, 0, 0, 0);
    return;
    }
    if (mOrientation == LinearLayoutManager.HORIZONTAL) {
    outRect.set(0, 0, 0, mDividerSize);
    } else {
    outRect.set(0, 0, mDividerSize, 0);
    }
    }

    //绘制分割线
    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    if (mOrientation == LinearLayoutManager.VERTICAL) {
    drawVertical(c, parent);
    } else {
    drawHorizontal(c, parent);
    }
    }

    //绘制横向 item 分割线
    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    final int left = parent.getPaddingLeft() + mOffsetStart;
    final int right = parent.getMeasuredWidth() - (parent.getPaddingRight() + mOffsetEnd);
    final int childSize = parent.getChildCount();
    for (int i = 0; i < childSize - 1; i++) {
    final View child = parent.getChildAt(i);

    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
    final int top = child.getBottom() + layoutParams.bottomMargin;
    final int bottom = top + mDividerSize;
    if (mDivider != null) {
    mDivider.setBounds(left, top, right, bottom);
    mDivider.draw(canvas);
    }
    if (mPaint != null) {
    canvas.drawRect(left, top, right, bottom, mPaint);
    }
    }
    }

    //绘制纵向 item 分割线
    private void drawVertical(Canvas canvas, RecyclerView parent) {
    final int top = parent.getPaddingTop() + mDividerSize;
    final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom() + mDividerSize;
    final int childSize = parent.getChildCount();
    for (int i = 0; i < childSize; i++) {
    final View child = parent.getChildAt(i);
    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
    final int left = child.getRight() + layoutParams.rightMargin;
    final int right = left + mDividerSize;
    if (mDivider != null) {
    mDivider.setBounds(left, top, right, bottom);
    mDivider.draw(canvas);
    }
    if (mPaint != null) {
    canvas.drawRect(left, top, right, bottom, mPaint);
    }
    }
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    举个例子:

    rcyDetailsDelegation.addItemDecoration(new RecyclerViewDivider(getContext(), LinearLayoutManager.HORIZONTAL,
    5, R.color.white, 8, 8));
    1
    2
    其中提供一个 RecyclerViewDivider:
    --------------------- 

  • 相关阅读:
    两个半成品的ORM
    Mayberry小镇的管理 | 三种截然不同的领导风格 3M
    敏捷的目的(方向)错了以后……
    Error:java: Compilation failed: internal java compiler error
    java: -source 1.5 中不支持 diamond 运算符 (请使用 -source 7 或更高版本以启用 diamond 运算符)
    看mybatis日志模块时涉及的动态代理
    看的顺眼的却Destination Unreachable
    如何下载钉钉回放视频
    不想学习时看一看会有帮助的,“但行好事,莫问前程”
    守护线程
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11069823.html
Copyright © 2011-2022 走看看