【Android -- UI 开发】Recyclerview 的通用分隔线

2023-12-29 22:30

本文主要是介绍【Android -- UI 开发】Recyclerview 的通用分隔线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

不断学习,做更好的自己!💪

视频号CSDN简书
欢迎打开微信,关注我的视频号:KevinDev点我点我

简介

ItemDecorationRecyclerView 的一个抽象静态内部类,负责装饰 Item 视图,例如添加间距、高亮或者分组边界等。

LinearLayoutManager 添加分割线

1. 效果图
在这里插入图片描述

2. 源码

public class DividerItemDecoration extends RecyclerView.ItemDecoration{private Context mContext;private Drawable mDivider;private int mOrientation;public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;//我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置public static final int[] ATRRS  = new int[]{android.R.attr.listDivider};public DividerItemDecoration(Context context, int orientation) {this.mContext = context;final TypedArray ta = context.obtainStyledAttributes(ATRRS);this.mDivider = ta.getDrawable(0);ta.recycle();setOrientation(orientation);}//设置屏幕的方向public void setOrientation(int orientation){if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST){throw new IllegalArgumentException("invalid orientation");}mOrientation = orientation;}@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {if (mOrientation == HORIZONTAL_LIST){drawVerticalLine(c, parent, state);}else {drawHorizontalLine(c, parent, state);}}//画横线, 这里的parent其实是显示在屏幕显示的这部分public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state){int left = parent.getPaddingLeft();int right = parent.getWidth() - parent.getPaddingRight();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++){final View child = parent.getChildAt(i);//获得child的布局信息final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();final int top = child.getBottom() + params.bottomMargin;final int bottom = top + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);//Log.d("wnw", left + " " + top + " "+right+"   "+bottom+" "+i);}}//画竖线public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state){int top = parent.getPaddingTop();int bottom = parent.getHeight() - parent.getPaddingBottom();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++){final View child = parent.getChildAt(i);//获得child的布局信息final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();final int left = child.getRight() + params.rightMargin;final int right = left + mDivider.getIntrinsicWidth();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}//由于Divider也有长宽高,每一个Item需要向下或者向右偏移@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {if(mOrientation == HORIZONTAL_LIST){//画横线,就是往下偏移一个分割线的高度outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());}else {//画竖线,就是往右偏移一个分割线的宽度outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);}}
}

3. 使用方法

		LinearLayoutManager manager = new LinearLayoutManager(this);mRecyclerView.setLayoutManager(manager);mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));mRecyclerView.setAdapter(mAdapter);

GridLayoutManager 添加分割线

1. 效果图
在这里插入图片描述

2. 源码

public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {private Context mContext;private int mFirstAndLastColumnW;          //您所需指定的间隔宽度,主要为第一列和最后一列与父控件的间隔;行间距,列间距将动态分配private int mFirstRowTopMargin = 0; //第一行顶部是否需要间隔private int mLastRowBottomMargin = 0;private int mSpanCount = 0;private int mScreenW = 0;private int mItemDistance;/*** @param firstAndLastColumnW 间隔宽度* @param firstRowTopMargin   第一行顶部是否需要间隔*/public GridDividerItemDecoration(Context context, int firstAndLastColumnW, int firstRowTopMargin, int lastRowBottomMargin) {mContext = context;mFirstAndLastColumnW = firstAndLastColumnW;mFirstRowTopMargin = firstRowTopMargin;mLastRowBottomMargin = lastRowBottomMargin;}public void setFirstRowTopMargin(int firstRowTopMargin) {mFirstRowTopMargin = firstRowTopMargin;}public void setLastRowBottomMargin(int lastRowBottomMargin) {mLastRowBottomMargin = lastRowBottomMargin;}public void setItemDistance(int itemDistance) {mItemDistance = itemDistance;}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);int top = 0;int left;int right;int bottom;int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();mSpanCount = getSpanCount(parent);int childCount = parent.getAdapter().getItemCount();// 屏幕宽度-View的宽度*spanCount 得到屏幕剩余空间int maxDividerWidth = getMaxDividerWidth(view);int spaceWidth = mFirstAndLastColumnW;//首尾两列与父布局之间的间隔// 除去首尾两列,item与item之间的距离int eachItemWidth = maxDividerWidth / mSpanCount;int dividerItemWidth = (maxDividerWidth - 2 * spaceWidth) / (mSpanCount - 1);//item与item之间的距离left = itemPosition % mSpanCount * (dividerItemWidth - eachItemWidth) + spaceWidth;right = eachItemWidth - left;bottom = dividerItemWidth;// 首行if (mFirstRowTopMargin > 0 && isFirstRow(parent, itemPosition, mSpanCount, childCount)) {top = mFirstRowTopMargin;}//最后一行if (isLastRow(parent, itemPosition, mSpanCount, childCount)) {if (mLastRowBottomMargin < 0) {bottom = 0;} else {bottom = mLastRowBottomMargin;}}outRect.set(left, top, right, bottom);}/*** 获取Item View的大小,若无则自动分配空间* 并根据 屏ge幕宽度-View的宽度*spanCount 得到屏幕剩余空间** @param view* @return*/private int getMaxDividerWidth(View view) {int itemWidth = view.getLayoutParams().width;int itemHeight = view.getLayoutParams().height;int screenWidth = getScreenWidth();int maxDividerWidth = screenWidth - itemWidth * mSpanCount;if (itemHeight < 0 || itemWidth < 0 || maxDividerWidth <= (mSpanCount - 1) * mFirstAndLastColumnW) {view.getLayoutParams().width = getAttachCloumnWidth();view.getLayoutParams().height = getAttachCloumnWidth();maxDividerWidth = screenWidth - view.getLayoutParams().width * mSpanCount;}return maxDividerWidth;}private int getScreenWidth() {if (mScreenW > 0) {return mScreenW;}mScreenW = mContext.getResources().getDisplayMetrics().widthPixels > mContext.getResources().getDisplayMetrics().heightPixels? mContext.getResources().getDisplayMetrics().heightPixels : mContext.getResources().getDisplayMetrics().widthPixels;return mScreenW;}/*** 根据屏幕宽度和item数量分配 item View的width和height** @return*/private int getAttachCloumnWidth() {int itemWidth = 0;int spaceWidth = 0;try {int width = getScreenWidth();spaceWidth = 2 * mFirstAndLastColumnW;itemWidth = (width - spaceWidth) / mSpanCount - 40;} catch (Exception e) {e.printStackTrace();}return itemWidth;}/*** 判读是否是第一列** @param parent* @param pos* @param spanCount* @return*/private boolean isFirstColumn(RecyclerView parent, int pos, int spanCount) {RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();if (layoutManager instanceof GridLayoutManager) {if (pos % spanCount == 0) {return true;}} else if (layoutManager instanceof StaggeredGridLayoutManager) {int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();if (orientation == StaggeredGridLayoutManager.VERTICAL) {if (pos % spanCount == 0) {// 第一列return true;}} else {}}return false;}/*** 判断是否是最后一列** @param parent* @param pos* @param spanCount* @return*/private boolean isLastColumn(RecyclerView parent, int pos, int spanCount) {RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();if (layoutManager instanceof GridLayoutManager) {if ((pos + 1) % spanCount == 0) {// 如果是最后一列,则不需要绘制右边return true;}} else if (layoutManager instanceof StaggeredGridLayoutManager) {int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();if (orientation == StaggeredGridLayoutManager.VERTICAL) {if ((pos + 1) % spanCount == 0) {// 最后一列return true;}} else {}}return false;}/*** 判读是否是最后一行** @param parent* @param pos* @param spanCount* @param childCount* @return*/private boolean isLastRow(RecyclerView parent, int pos, int spanCount, int childCount) {RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();if (layoutManager instanceof GridLayoutManager) {int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;return lines == pos / spanCount + 1;}return false;}/*** 判断是否是第一行** @param parent* @param pos* @param spanCount* @param childCount* @return*/private boolean isFirstRow(RecyclerView parent, int pos, int spanCount, int childCount) {RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();if (layoutManager instanceof GridLayoutManager) {if ((pos / spanCount + 1) == 1) {return true;} else {return false;}}return false;}/*** 获取列数** @param parent* @return*/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();}return spanCount;}
}

3. 使用方法

		int firstAndLastColumnW = DensityUtil.dip2px(this, 20);int firstRowTopMargin = DensityUtil.dip2px(this, 20);GridDividerItemDecoration gridDividerItemDecoration =new GridDividerItemDecoration(this, firstAndLastColumnW, firstRowTopMargin, firstRowTopMargin);gridDividerItemDecoration.setFirstRowTopMargin(firstRowTopMargin);gridDividerItemDecoration.setLastRowBottomMargin(firstRowTopMargin);mRecyclerView.addItemDecoration(gridDividerItemDecoration);GridLayoutManager layoutManager = new GridLayoutManager(this, 3);mRecyclerView.setLayoutManager(layoutManager);mRecyclerView.setAdapter(mAdapter);

这篇关于【Android -- UI 开发】Recyclerview 的通用分隔线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/550908

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

【区块链 + 人才服务】区块链集成开发平台 | FISCO BCOS应用案例

随着区块链技术的快速发展,越来越多的企业开始将其应用于实际业务中。然而,区块链技术的专业性使得其集成开发成为一项挑战。针对此,广东中创智慧科技有限公司基于国产开源联盟链 FISCO BCOS 推出了区块链集成开发平台。该平台基于区块链技术,提供一套全面的区块链开发工具和开发环境,支持开发者快速开发和部署区块链应用。此外,该平台还可以提供一套全面的区块链开发教程和文档,帮助开发者快速上手区块链开发。