服务器之家

服务器之家 > 正文

Android自定义上下左右间隔线

时间:2022-03-02 15:26     来源/作者:u014469691

本文实例为大家分享了Android自定义SpaceItemDecoration分割线的具体代码,供大家参考,具体内容如下

此线是在RecyclerView中的GridlayoutManager中使用的 效果图如下:

Android自定义上下左右间隔线

?
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
/**
 * author: Y_Qing
 * date: 2018/5/7
 */
 
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
  private int space;
  Paint mPaint;
 
  public SpaceItemDecoration(int space) {
    this.space = space;
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setColor(0xffededed);
    mPaint.setStyle(Paint.Style.FILL);
  }
 
  private static final int HORIZONTAL = 355;
 
  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int itemViewType = parent.getLayoutManager().getItemViewType(view);
    if (itemViewType == HORIZONTAL) { //根据ViewType类型来判断是否展示线
      outRect.bottom = space;
      if (parent.getChildLayoutPosition(view) % 2 == 0) {
        outRect.right = space / 2;
        outRect.left = space;
      } else {
        outRect.right = space;
        outRect.left = space / 2;
      }
    }
  }
 
  @Override
  public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    c.save();
    if (parent.getLayoutManager() instanceof GridLayoutManager) {
      if ((((GridLayoutManager) parent.getLayoutManager())).getSpanCount() == 2) {
        draw(c, parent);
      }
    }
    c.restore();
 
  }
 
  //绘制横向 item 分割线
  private void draw(Canvas canvas, RecyclerView parent) {
    int childSize = parent.getChildCount();
    for (int i = 0; i < childSize; i++) {
      View child = parent.getChildAt(i);
      RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
      int left = child.getLeft();
      int right = child.getRight();
      int top = child.getBottom() + layoutParams.bottomMargin;
      int bottom = top + space;
      if (mPaint != null) {
        canvas.drawRect(left, top, right, bottom, mPaint);//绘制图片下放的水平线
      }
      if (parent.getChildLayoutPosition(child) % 2 == 0) {
        canvas.drawRect(0, child.getTop(), left, child.getBottom() + space, mPaint);//绘制左边图片的左间线
      }
      top = child.getTop();
      bottom = child.getBottom() + space;
      left = child.getRight() + layoutParams.rightMargin;
      right = left + space;
      if (mPaint != null) {
        canvas.drawRect(left, top, right, bottom, mPaint);//绘制图片的右间线
      }
    }
  }
 
}

使用方法下:

?
1
2
3
4
5
6
private void initSpaceLine() {
    if (decor == null) {
      decor = new SpaceItemDecoration(ViewUtil.dp2px(5));
      recycleListView.addItemDecoration(decor);
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u014469691/article/details/80666215

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部