android recyclerview item间距,自定义RecyclerView.ItemDecoration,实现Item的等间距分割以及分割线效果...

本文介绍如何在Android中自定义RecyclerView.ItemDecoration,实现网格和线性布局管理器下的Item间距和分割线效果。通过SpacesItemDecoration类,覆盖onDraw和getItemOffsets方法,针对LinearLayoutManager和GridLayoutManager分别实现不同布局类型的分割线。此外,还展示了如何添加颜色,以创建带颜色的分割线效果。
摘要由CSDN通过智能技术生成

1.背景

RecyclerView 是谷歌 V7 包下新增的控件,用来替代 ListView 和 GridView 使用的一个控件。在使用的过程中,往往需要使用到 divider 的效果 ( item 之间的分割线 )。而 RecyclerView 并不像 ListView 一样自带有 divider 的属性。而是需要用到 RecyclerView.ItemDecoration 这样一个类,但是 ItemDecoration 是一个抽象类,而且 android 内部并没有给它做一些效果的实现。那么就需要我们自己去继承并实现其中的方法,本文讲述的就是在 GridLayoutManager 和 LinearLayoutManager 下如何去实现 ItemDecoration。至于 RecyclerView.ItemDecoration 的具体分析,大家可以去看看这篇文章 深入理解 RecyclerView 系列之一:ItemDecoration 这里不作过多的阐述。

2.实现基本的 Item 的 divider

2.1 创建 SpacesItemDecoration

创建一个类 SpacesItemDecoration 继承于 RecyclerView.ItemDecoration ,实现其中的 onDraw 和 getItemOffsets 方法,在这里我们的设计是左右距离相等,上下距离相等。

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

private int leftRight;

private int topBottom;

public SpacesItemDecoration(int leftRight, int topBottom) {

this.leftRight = leftRight;

this.topBottom = topBottom;

}

@Override

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

super.onDraw(c, parent, state);

}

@Override

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

}

}

在这里我们主要实现的方法是 onDraw 和 getItemOffsets, getItemOffsets 主要是确定 divider 的范围,而 onDraw 是对 divider 的具体实现。

2.2 LinearLayoutManager 下 divider 的实现

首先在 getItemOffsets 方法中需要判断当前的 RecyclerView 所采用的哪种 LayoutManager。这里要注意的是 GridLayoutManager 是继承 LinearLayoutManager 的,所以需要先判断是否为 GridLayoutManager。

private SpacesItemDecorationEntrust getEntrust(RecyclerView.LayoutManager manager) {

SpacesItemDecorationEntrust entrust = null;

//要注意这边的GridLayoutManager是继承LinearLayoutManager,所以要先判断GridLayoutManager

if (manager instanceof GridLayoutManager) {

entrust = new GridEntrust(leftRight, topBottom, mColor);

} else {//其他的都当做Linear来进行计算

entrust = new LinearEntrust(leftRight, topBottom, mColor);

}

return entrust;

}

然后我们来看具体的实现,首先判断是 VERTICAL 还是 HORIZONTAL 。对于 VERTICAL,每一个 item 必需的是 top,left 和 right,但是最后一个 item 还需要 bottom。而对于 HORIZONTAL ,每一个 item 必需的是 top,left 和 bottom,但是最后一个 item 还需要 right。

@Override

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();

//竖直方向的

if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {

//最后一项需要 bottom

if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {

outRect.bottom = topBottom;

}

outRect.top = topBottom;

outRect.left = leftRight;

outRect.right = leftRight;

} else {

//最后一项需要right

if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {

outRect.right = leftRight;

}

outRect.top = topBottom;

outRect.left = leftRight;

outRect.bottom = topBottom;

}

}

就这样,divider 效果就实现了(当然是没有任何的颜色的)。调用方式只需要。

int leftRight = dip2px(7);

int topBottom = dip2px(7);

rv_content.addItemDecoration(n

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值