RecyclerView消除底部分割线的方法
作者:掌握当下 时间:2022-05-24 06:48:45
最近遇到一个问题,用RecyclerView显示数据,纵向列表显示,添加默认分割线。
问题是:底部也会显示分割线,这很影响美观。
怎么解决这个问题呢?我想了很多办法,毫无头绪。。。
最后,查看默认分割线的类DividerItemDecoration的源码:
public class DividerItemDecoration extends ItemDecoration {
private static final int[] ATTRS = new int[]{16843284};
public static final int HORIZONTAL_LIST = 0;
public static final int VERTICAL_LIST = 1;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
TypedArray a = context.obtainStyledAttributes(ATTRS);
this.mDivider = a.getDrawable(0);
a.recycle();
this.setOrientation(orientation);
}
public void setOrientation(int orientation) {
if(orientation != 0 && orientation != 1) {
throw new IllegalArgumentException("invalid orientation");
} else {
this.mOrientation = orientation;
}
}
public void onDraw(Canvas c, RecyclerView parent) {
if(this.mOrientation == 1) {
this.drawVertical(c, parent);
} else {
this.drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for(int i = 0; i < childCount; ++i) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams)child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + this.mDivider.getIntrinsicHeight();
this.mDivider.setBounds(left, top, right, bottom);
this.mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
int childCount = parent.getChildCount();
for(int i = 0; i < childCount; ++i) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams)child.getLayoutParams();
int left = child.getRight() + params.rightMargin;
int right = left + this.mDivider.getIntrinsicHeight();
this.mDivider.setBounds(left, top, right, bottom);
this.mDivider.draw(c);
}
}
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if(this.mOrientation == 1) {
outRect.set(0, 0, 0, this.mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, this.mDivider.getIntrinsicWidth(), 0);
}
}
}
因为我用到的是垂直列表,用到的是红色字体处的代码:
public void drawVertical(Canvas c, RecyclerView parent) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for(int i = 0; i < childCount; ++i) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams)child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + this.mDivider.getIntrinsicHeight();
this.mDivider.setBounds(left, top, right, bottom);
this.mDivider.draw(c);
}
}
从代码中很容易看出只要修改for循环中的内容就可去掉底部的分割线:
public void drawVertical(Canvas c, RecyclerView parent) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for(int i = 0; i < childCount-1; ++i) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams)child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + this.mDivider.getIntrinsicHeight();
this.mDivider.setBounds(left, top, right, bottom);
this.mDivider.draw(c);
}
}
因为这个类我们不能直接修改,所以我们可以自定义一个类,修改相应内容,添加分割线的时候,使用自定义类。
标签:RecyclerView,分割线
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ShardingSphere jdbc实现分库分表核心概念详解
2023-11-24 12:09:45
![](https://img.aspxhome.com/file/2023/3/59763_0s.png)
SpringBoot集成Swagger2的方法
2023-11-26 13:15:42
![](https://img.aspxhome.com/file/2023/0/60350_0s.png)
c#实现输出的字符靠右对齐的示例
2023-02-26 12:23:39
![](https://img.aspxhome.com/file/2023/0/82140_0s.jpg)
Java下http下载文件客户端和上传文件客户端实例代码
2021-09-09 16:52:11
Android框架Volley使用:ImageRequest请求实现图片加载
2022-03-01 11:30:06
![](https://img.aspxhome.com/file/2023/5/113325_0s.jpg)
.net中前台javascript与后台c#函数相互调用问题
2022-06-02 15:49:16
Swing常用组件之多行文本区JTextArea
2023-11-08 14:16:49
C#使用iTextSharp将PDF转成文本的方法
2022-05-03 16:59:48
SpringBoot Data JPA 关联表查询的方法
2021-08-08 13:59:23
![](https://img.aspxhome.com/file/2023/3/96063_0s.png)
浅谈Java工程读取resources中资源文件路径的问题
2021-07-20 19:13:45
![](https://img.aspxhome.com/file/2023/8/67668_0s.png)
C#多线程之线程池ThreadPool详解
2021-10-30 23:59:45
Spring Boot集成Redis实现缓存机制(从零开始学Spring Boot)
2023-03-21 15:11:38
Java中的这些骚操作你不能不知道!!!
2022-07-08 12:28:14
![](https://img.aspxhome.com/file/2023/4/60574_0s.png)
Android教你如何发现APP卡顿的实现
2022-06-10 22:33:45
C#基于OLEDB获取Excel文件表结构信息的方法
2022-03-06 07:29:44
Java多线程编程之访问共享对象和数据的方法
2022-10-01 07:05:02
Java实现月饼的制作、下单和售卖功能
2023-03-06 18:26:24
![](https://img.aspxhome.com/file/2023/4/85574_0s.png)
QT5实现简单的TCP通信的实现
2023-11-02 21:24:48
![](https://img.aspxhome.com/file/2023/1/105171_0s.png)
MyBatis之自查询使用递归实现 N级联动效果(两种实现方式)
2023-04-15 03:34:50
![](https://img.aspxhome.com/file/2023/9/63789_0s.png)
spring boot mybatis枚举映射示例代码
2023-03-01 11:45:51