RecycleView设置顶部分割线(记录一个坑)
大家都知道,想给RecycleView设置分割线可以重写RecyclerView.ItemDecoration
项目过程中,遇到一个需求:RecycleView顶部有一条灰色的间隔,我想到了给RecycleView设置分割线的方法,当然只给第一个Item设置,而且在上方。
public class MyDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
/**
* Custom divider will be used
*/
public MyDividerItemDecoration(Context context, int resId) {
mDivider = ContextCompat.getDrawable(context, resId);
}
public DividerItemDecoration(Drawable drawable) {
mDivider = drawable;
}
@Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
//因为绘制了顶部分割线,因此需要将第一行的item下移相应的距离
//这里要注意,判断该RecycleView是什么布局,如果是竖直方向上的线性布局(水平方向上的这里没有考虑)
//,那么需要让第一个Item下移,移动的距离是分割线的高度,因为分割线会占据Item的空间
//如果是网格布局,那么需要把第一行的所有Item都下移相应的高度
if (layoutManager instanceof LinearLayoutManager) {
if (parent.getChildAdapterPosition(view) == 0) {
outRect.set(0, mDivider.getIntrinsicHeight(), 0, 0);
}
}
if (layoutManager instanceof GridLayoutManager) {
if (parent.getChildAdapterPosition(view) >= 0 &&
parent.getChildAdapterPosition(view) < getSpanCount(parent)) {
outRect.set(0, mDivider.getIntrinsicHeight(), 0, 0);
}
}
} @Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { //绘制分割线
int left = 0;
int right = parent.getWidth();
View child = parent.getChildAt(0); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); //不可以直接设置top = 0;因为这样的话分隔线就不会跟着移动,因为top = 0,是绝对位置,
//所以应该设置为子view的相对位置
//这样才可以跟着滑动。
//child的顶部坐标,减去设置的margin_top的值,再减去child为了给分割线腾出空间所下滑的高度,
//这样分割线才会在顶部
int top = child.getTop() - params.topMargin - mDivider.getIntrinsicHeight();
int bottom;
bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
} private int getSpanCount(RecyclerView parent) {
// 列数
int spanCount = -1;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
spanCount = ((StaggeredGridLayoutManager) layoutManager)
.getSpanCount();
} else if (layoutManager instanceof LinearLayoutManager) {
spanCount = layoutManager.getItemCount();
}
return spanCount;
}
在onDrawOver方法中可以绘制分割线。
这里有一个需要注意的坑,调试了很久,最终才发现,难受。
在onDrawOver里面,一开始我设置top = 0;因为绘制在顶部嘛。结果出现了一个现象,顶部分割线一直停留在顶部,不会跟着移动。最后改为int top = child.getTop() - params.topMargin - mDivider.getIntrinsicHeight();才成功了。为什么呢?
因为直接写top = 0;这是绝对位置了,要让分割线也跟着滑动,需要用的是相对位置,相对于item的位置,这样才能够跟着item滑动。
调用:
recyclerView.addItemDecoration(new MyDividerItemDecoration(this, R.drawable.item_decoration));
item_decoration代码如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="10dp" />
<solid android:color="@color/comic_gray_bg" />
</shape>
或者我们可以直接代码中创建Drawable,然后传进去:
//添加白色分割线在顶部
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(ContextCompat.getColor(this, R.color.white));
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setSize(0, 3);
recyclerView.addItemDecoration(new DividerItemDecoration(drawable));
尊重劳动成果,转载请标明出处:https://www.cnblogs.com/tangZH/p/9489486.html
RecycleView设置顶部分割线(记录一个坑)的更多相关文章
- 关于pyinstaller打包程序时设置icon时的一个坑
关于pyinstaller打包程序时设置icon时的一个坑 之前在用pyinstaller打包程序的时候遇到了关于设置图标的一点小问题,无论在后面加--icon 或是-i都出现报错.查了下st ...
- allegro设置内存分配器的一个坑
看过<游戏引擎架构>后我开始对内存的分配问题关注,一直想用内存分配器来管理游戏的内存.前两天发现了有许多第三方内存分配器可以用.最后挑中了nedmalloc,这个库也是ogre所使用的,测 ...
- 记录我开发工作中遇到HTTP跨域和OPTION请求的一个坑
我通过这篇文章把今天工作中遇到的HTTP跨域和OPTION请求的一个坑记录下来. 场景是我需要在部署在域名a的Web应用里用JavaScript去消费一个部署在域名b的服务器上的服务.域名b上的服务也 ...
- 再次记录 Visual Studio 2015 CTP 5 的一个坑
接上一篇:升级 Visual Studio 2015 CTP 5 的坑.坑.坑 升级到 VS2015 CTP 之后,今天要改项目中的一个东西,然后就不得不把 C# 6.0 改变的语法代码中改了下(之前 ...
- 记录js new Date日期处理的一个坑
记录js日期处理的一个坑 当前时区为北美东部时区时, new Date('2019-4-1') new Date('2019-04-01') 结果是相关一个月的. 如下图 new Date(' ...
- Android中隐藏顶部状态栏的那些坑——Android开发之路3
Android中隐藏顶部状态栏的那些坑 先看看常规的隐藏状态栏的方法: 方法一: @Override protected void onCreate(Bundle savedInstanceState ...
- andriod8.1.0源码编译中的一个坑-package com.sun.javadoc does not exist
这里记录编译过程中的一个坑!!! 编译过程中出现了下面的报错 external/doclava/src/com/google/doclava/ClassInfo.java:20: error: pac ...
- 【转载】linux命令行计算器bc的一个“坑”
[转载自]http://blog.chinaunix.net/uid-174325-id-3518953.html 结论:ibase,obase可以使用在不同的计算公式里,但是尽量把obase放iba ...
- 使用ffmpeg视频编码过程中踩的一个坑
今天说说使用ffmpeg在写视频编码程序中踩的一个坑,这个坑让我花了好多时间,回头想想,非常多时候一旦思维定势真的挺难突破的.以下是不对的编码结果: ...
随机推荐
- APP测试流程的总结
本规范基于app大小版本测试经验总结. 第一阶段:需求分析(技术+产品) 1. 新需求是否合理 2. 新旧需求时否存在冲突 3. 理出测试重点 4. 估算测试时间 5. 不熟悉的需求点,确认(负责人, ...
- 快速理解Token,Cookie,Session
在Web应用中,HTTP请求是无状态的.即:用户第一次发起请求,与服务器建立连接并登录成功后,为了避免每次打开一个页面都需要登录一下,就出现了cookie,Session. Cookie Cookie ...
- Kali学习笔记41:SQL手工注入(3)
前两篇文章都是基于目标系统允许union,order by语句 并且可以读取infomation_schema元数据库 如果遇到的是安全方面做得很好的应用,进行了权限限制,那么我们有什么办法呢? 猜测 ...
- [Swift]LeetCode108. 将有序数组转换为二叉搜索树 | Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [Swift]LeetCode240. 搜索二维矩阵 II | Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [Swift]LeetCode650. 只有两个键的键盘 | 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- Linux之相关英文缩写含义
1.目录名: 名称 英文 英文含义 描述 /boot boot 引导 操作系统的内核及在引导过程中使用的文件 /root root 根 系统管理员的主目录(根目录) /run run 运行 系统运行时 ...
- xtrabackup备份(MySQL备份)与恢复
xtrabackup备份(MySQL备份)与恢复 1. innobackupex参数选项 --no-timestamp: 不创建一个时间戳 --defaults-file=[MY.CNF] //指定配 ...
- Javascript 链式操作以及流程控制
春节过后,感觉过年吃的油腻的食品转化的脂肪都长到 脑子去了. 根本转不动啊 上班第一天 实在是写不动代码了, 顺手打开多天为看的 收件箱,查看查看邮件,看看春节期间 风云变幻的前端圈又有哪些大事发生. ...
- 反射在ADO.NET方面的应用
本来说通过传统的方式可以很方便的访问数据库不需要用到反射技术,但是为了将反射在ADO.NET中的作用体现出来,特意来试一下改良版的访问方式. 反射的学习需要一定的时间去理解,我学了有一阵子了,但也不 ...