RecyclerView消除底部分割线的方法
作者:掌握当下 发布时间:2022-05-24 06:48:45
标签:RecyclerView,分割线
最近遇到一个问题,用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);
}
}
因为这个类我们不能直接修改,所以我们可以自定义一个类,修改相应内容,添加分割线的时候,使用自定义类。


猜你喜欢
- 解析:如果并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象,那么可以使用Java集合框架。如果启用集合的删除方法,那么集合中所有
- 1. interrupt知识点 以下总结基于JDK8本文不会完整说明interrupt,只会罗列一些比较重要的点。完整了解Thre
- 1、spring aop实现首先application-test.yml增加如下数据源的配置spring: datasource
- 前言基于SpingBoot框架中, 我们随处可以见的便是各种各样的功能注解, 注解的实现原理AOP之前有说过(翻看本系列的前面几章即可),
- 一、事务隔离级别①介绍数据库系统必须具有隔离并发运行各个事务的能力,使它们不会相互影响,避免各种并发问题。一个事 务与其他事务隔离的程度称为
- 在logback.xml中加上该配置,包名如:com.xxx<logger name="packageName"
- 今天写Tab的时候由于TAB的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将TAB放置在屏幕的底端。 <?xml version
- 前言最近看了一下 Android 上的图表控件,去年做过一款应用也已上架了,也用到了图表控件,但是只是按照官方 demo 集成了,并没有过多
- 本文实例讲述了Android实现ListView异步加载的方法。分享给大家供大家参考,具体如下:@Override publi
- LRU简介LRU是Least Recently Used 近期最少使用算法,它就可以将长时间没有被利用的数据进行删除。实现最近面了阿里的外包
- 本文实例为大家分享了C#十五子游戏的具体代码,供大家参考,具体内容如下using System;using System.Collectio
- 前言本文主要给大家介绍的是java虚拟机的故障处理工具,文中提到这些工具包括:名称主要作用jpsJVM process Status Too
- 什么原因使我们不得不使用线程池?个人认为主要原因是:短时间内需要处理的任务数量很多使用线程池的好处:1.减少在创建和销毁线程上所花的时间以及
- 前言在Android开发过程中,我发现很多安卓源代码里应用了设计模式,比较常用的有适配器模式(各种adapter),建造者模式(Alert
- 本文实例讲述了C#控制图像旋转和翻转的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Co
- 前言我们在写搬砖的过程中,少不了需要将A对象转成B对象,对对象进行对象的转换是一个操作重复且繁琐的工作。于是市面上就有许多的对象转换工具来解
- 需求场景:当数据库中保存的部分数据需要加密,页面需要正常显示时。这是就需要我们自定义类型转换器,在Mybatis执行SQL得到结果时,通过自
- 废话不多说了直接给大家贴代码了,具体代码如下所示:<?xml version="1.0" encoding=&qu
- 本文讲述了Java实现画线、矩形、椭圆、字符串功能的实例代码。分享给大家供大家参考,具体如下:import java.awt.Frame;
- 概述主要用于Java线程里指定时间或周期运行任务。Timer是线程安全的,但不提供实时性(real-time)保证。构造函数Timer()默