当前位置:主页 > 移动开发 > Android代码 >

Android 给RecyclerView添加分割线的具体步骤(分享)

时间:2023-02-04 11:15:47 | 栏目:Android代码 | 点击:

【吐槽】RecyclerView没有提供分割线的方法,想要加个线还要自己画,点击事件的监听都要自己实现,不过真的好用。

给RecyclerView添加分割线的步骤

1、新建类继承于RecyclerView.ItemDecoration,此为是抽象类:

  public static abstract class ItemDecoration {

  public void onDraw(Canvas c, RecyclerView parent, State state) {
    onDraw(c, parent);
  }

  public void onDrawOver(Canvas c, RecyclerView parent, State state) {
    onDrawOver(c, parent);
  }

  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
    getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),
        parent);
  }

}

“onDraw和onDrawOver,显然,这两个方法是用于绘制的,那么绘制分割线的逻辑可以放在这里面,它们二者的具体区别是:onDraw是在item view绘制之前调用,而onDrawOver是在item view绘制之后调用,因此我们一般选择重写其中一个方法即可。getItemOffsets,这个方法是告诉RecyclerView在绘制完一个item view的时候,应该留下多少空位,以便于绘制分割线。”

好像逻辑也不难,但是真的好麻烦,比如我要给我的瀑布流布局加一条系统自带的分割线,系统自带的就行,那我要新建一个实现类:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    //使用系统自带的listDivider
    private static final int[] ATTRS = new int[]{
        android.R.attr.listDivider
    };

    private Drawable mDivider;

    public DividerItemDecoration(Context context) {
      super();
      // 从TypedArray中得到一个Drawable对象
      final TypedArray typedArray = context.obtainStyledAttributes(ATTRS);
      mDivider = typedArray.getDrawable(0);

      typedArray.recycle();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
      super.onDraw(c, parent, state);
      drawDivider(c, parent);
    }

    /**
     * 遍历childView,为每一个childView描绘divider
     * @param c 画布对象
     * @param parent 父控件即RecyclerView
     */
    private void drawDivider(Canvas c, RecyclerView parent) {
      //获取分割线的上边距,即RecyclerView的padding值
      final int top = parent.getPaddingTop();

      //分割线下边距
      final int bottom = parent.getHeight() - parent.getPaddingBottom();

      final int childCount = parent.getChildCount();

      //遍历所有item view,为它们的右边方绘制分割线,就是计算出上下左右四个值画一个矩形
      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.getIntrinsicWidth();
        //画右边的divider
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);

        //画完右边画底边
        mDivider.setBounds(child.getLeft() + child.getPaddingLeft(), child.getBottom() + params.bottomMargin,
            child.getRight() - child.getPaddingRight(), child.getBottom() + mDivider.getIntrinsicHeight());
        mDivider.draw(c);
      }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
      super.getItemOffsets(outRect, view, parent, state);
      outRect.set(0, 0, mDivider.getIntrinsicHeight(), mDivider.getIntrinsicHeight());
    }
  }

2、为RecyclerView添加写好的ItemDecoration即可

mRecyclerView.addItemDecoration(new DividerItemDecoration(this));

3、没有3。

其实从 RecyclerView.ItemDecoration 的名字就看得出来,它是RecyclerView的item的装饰品,也就是说,除了画分割线,想画什么都是可以的。

您可能感兴趣的文章:

相关文章