Android应用开发—通用的GridView网格分割线

注:本文基于 Android RecyclerView 使用完全解析 体验艺术般的控件 中关于GridView网格分割线部分代码扩展而来。

原接口代码:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.RecyclerView.State;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {private static final int[] ATTRS = new int[]{android.R.attr.listDivider};private Drawable mDivider;public DividerGridItemDecoration(Context context) {final TypedArray a = context.obtainStyledAttributes(ATTRS);mDivider = a.getDrawable(0);a.recycle();}@Overridepublic void onDraw(Canvas c, RecyclerView parent, State state) {drawHorizontal(c, parent);drawVertical(c, parent);}private int getSpanCount(RecyclerView parent) {// 列数int spanCount = -1;LayoutManager layoutManager = parent.getLayoutManager();if (layoutManager instanceof GridLayoutManager) {spanCount = ((GridLayoutManager) layoutManager).getSpanCount();} else if (layoutManager instanceof StaggeredGridLayoutManager) {spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();}return spanCount;}public void drawHorizontal(Canvas c, RecyclerView parent) {int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int left = child.getLeft() - params.leftMargin;final int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth();final int top = child.getBottom() + params.bottomMargin;final int bottom = top + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}public void drawVertical(Canvas c, RecyclerView parent) {final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int top = child.getTop() - params.topMargin;final int bottom = child.getBottom() + params.bottomMargin;final int left = child.getRight() + params.rightMargin;final int right = left + mDivider.getIntrinsicWidth();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {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 {childCount = childCount - childCount % spanCount;if (pos >= childCount)// 如果是最后一列,则不需要绘制右边return true;}}return false;}private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {LayoutManager layoutManager = parent.getLayoutManager();if (layoutManager instanceof GridLayoutManager) {childCount = childCount - childCount % spanCount;if (pos >= childCount)// 如果是最后一行,则不需要绘制底部return true;} else if (layoutManager instanceof StaggeredGridLayoutManager) {int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();// StaggeredGridLayoutManager 且纵向滚动if (orientation == StaggeredGridLayoutManager.VERTICAL) {childCount = childCount - childCount % spanCount;// 如果是最后一行,则不需要绘制底部if (pos >= childCount)return true;} else {// 如果是最后一行,则不需要绘制底部if ((pos + 1) % spanCount == 0) {return true;}}}return false;}@Overridepublic void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {int spanCount = getSpanCount(parent);int childCount = parent.getAdapter().getItemCount();// 如果是最后一行,则不需要绘制底部if (isLastRaw(parent, itemPosition, spanCount, childCount)) {outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);} else if (isLastColum(parent, itemPosition, spanCount, childCount)) {// 如果是最后一列,则不需要绘制右边outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());} else {outRect.set(0, 0, mDivider.getIntrinsicWidth(),mDivider.getIntrinsicHeight());}}
}

扩展后的代码:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.RecyclerView.State;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {private static final int[] ATTRS = new int[]{android.R.attr.listDivider};private Drawable mDivider;private Paint mColorPaint;private int mDividerHeight = 1;public DividerGridItemDecoration(Context context) {final TypedArray a = context.obtainStyledAttributes(ATTRS);mDivider = a.getDrawable(0);a.recycle();}public DividerGridItemDecoration(Context context, int color, int height) {this(context);mColorPaint = new Paint();mColorPaint.setColor(color);mDividerHeight = height;}@Overridepublic void onDraw(Canvas c, RecyclerView parent, State state) {drawHorizontal(c, parent);drawVertical(c, parent);}private int getSpanCount(RecyclerView parent) {// 列数int spanCount = -1;LayoutManager layoutManager = parent.getLayoutManager();if (layoutManager instanceof GridLayoutManager) {spanCount = ((GridLayoutManager) layoutManager).getSpanCount();} else if (layoutManager instanceof StaggeredGridLayoutManager) {spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();}return spanCount;}public void drawHorizontal(Canvas c, RecyclerView parent) {int childCount = parent.getChildCount();int spanCount = getSpanCount(parent);int count = 0;if (childCount % spanCount != 0) {count = childCount;} else {count = childCount - spanCount;}for (int i = 0; i < count; i++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int left = child.getLeft() - params.leftMargin;final int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth();final int top = child.getBottom() + params.bottomMargin;final int bottom = top + mDivider.getIntrinsicHeight();if (mColorPaint != null) {c.drawRect(left, top, right, top + mDividerHeight, mColorPaint);} else {mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}}public void drawVertical(Canvas c, RecyclerView parent) {final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int top = child.getTop() - params.topMargin;final int bottom = child.getBottom() + params.bottomMargin;final int left = child.getRight() + params.rightMargin;final int right = left + mDivider.getIntrinsicWidth();if (mColorPaint != null) {c.drawRect(left, top, left + mDividerHeight, bottom, mColorPaint);} else {mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}}private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {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 {childCount = childCount - childCount % spanCount;if (pos >= childCount)// 如果是最后一列,则不需要绘制右边return true;}}return false;}private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {LayoutManager layoutManager = parent.getLayoutManager();if (layoutManager instanceof GridLayoutManager) {childCount = childCount - childCount % spanCount;if (pos >= childCount)// 如果是最后一行,则不需要绘制底部return true;} else if (layoutManager instanceof StaggeredGridLayoutManager) {int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();// StaggeredGridLayoutManager 且纵向滚动if (orientation == StaggeredGridLayoutManager.VERTICAL) {childCount = childCount - childCount % spanCount;// 如果是最后一行,则不需要绘制底部if (pos >= childCount)return true;} else {// 如果是最后一行,则不需要绘制底部if ((pos + 1) % spanCount == 0) {return true;}}}return false;}@Overridepublic void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {int spanCount = getSpanCount(parent);int childCount = parent.getAdapter().getItemCount();// 如果是最后一行,则不需要绘制底部if (isLastRaw(parent, itemPosition, spanCount, childCount)) {outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);} else if (isLastColum(parent, itemPosition, spanCount, childCount)) {// 如果是最后一列,则不需要绘制右边outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());} else {outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());}}
}

扩展的内容:

  • 最后一行不画分割线。通常我们的需求是边框都不进行分割的绘制,而原来的代码没有做这部分的处理。(原接口只处理了最后一列的情况)
  • 支持分割线的颜色和宽度绘制的定制化。(原接口只支持系统属性分割线的绘制)。绘制方式:DividerGridItemDecoration(Context context, int color, int height) 通过传递color和height进行绘制定制化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/450177.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hdfs部署

一、下载Hadoop 2.6.0-cdh5.7.0的tar.gz包并解压&#xff1a; wget http://archive.cloudera.com/cdh5/cdh/5/hadoop-2.6.0-cdh5.7.0.tar.gz tar -zxvf hadoop-2.6.0-cdh5.7.0.tar.gz cd /usr/local/hadoop-2.6.0-cdh5.7.0/ ls bin bin-mapreduce1 cloudera etc examples …

JVM技术周报第2期

JVM技术周报第2期 JVM技术周报分享JVM技术交流群的讨论内容&#xff0c;由群内成员整理归纳而成。如果你有兴趣入群讨论&#xff0c;请关注「Java技术精选」公众号&#xff0c;通过右下角菜单「入群交流」加我好友&#xff0c;获取入群详情。 1、如何阅读源码&#xff1f; 在我…

最长重复子串和最长不重复子串求解

最长重复子串和最长不重复子串求解 本文内容框架&#xff1a; 1 最长重复子串 基本方法、KMP算法求解、后缀数组求解 2 最长不重复子串 基本方法、动态规划、动态规划Hash 3 小结 1最长重复子串 1.1问题描述 首先这是一个单字符串问题。子字符串R 在字符串L 中至少出现两…

sql 查询结果为null替换 为 0

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 mysql : ifnull( xxx , 0 ) select ifnull(colname,0) from tablename oracle: NVL&#xff08;xxx , 0 )

前后端分离——token超时刷新策略

前言 记录一下前后端分离下————token超时刷新策略&#xff01; 需求场景 昨天发了一篇记录 前后端分离应用——用户信息传递 中介绍了token认证机制&#xff0c;跟几位群友讨论了下&#xff0c;有些同学有这么一个疑惑&#xff1a;token失效了&#xff0c;应该怎么做&…

Android ImageView的scaleType(图片比例类型)属性与adjustViewBounds(调整视图边界)属性

本文转载自[Android ImageView的scaleType&#xff08;图片比例类型&#xff09;属性与adjustViewBounds&#xff08;调整视图边界&#xff09;属性]并做了排版的修改(http://www.cnblogs.com/xzbc/p/5098347.html) Android ImageView的scaleType的属性可设置为matrix&#xff…

阿里云@广东:让我们一起上云!

锅炉故障预测 预警准确率95%以上 利用算法模型进行制程能力的 综合分析与评估优化 提升定标效率 时间降低30% 构建垃圾焚烧发电工艺的优化算法模型 综合考虑环保和设备负荷因素 推荐最优工艺参数组合 优化人工操作&#xff0c;提高燃烧效率 中海油能源发展公司 石油钻探预测优化…

程序员的自我经营

程序员的自我经营之道第一层 从你决定迈出校门进入社会的那一刻起&#xff0c;你就进入了一个战场&#xff0c;这个战场虽然没有硝烟弥漫&#xff0c;没有炮火纷飞&#xff0c;但却绝不亚于任何一个真正的战场。你要在这个战场上去搏杀&#xff0c;去竞争&#xff0c;利用各种…

P3966 [TJOI2013]单词

\(\color{#0066ff}{ 题目描述 }\) 小张最近在忙毕设,所以一直在读论文。一篇论文是由许多单词组成但小张发现一个单词会在论文中出现很多次,他想知道每个单词分别在论文中出现了多少次。 \(\color{#0066ff}{输入格式}\) 第一行一个整数N,表示有N个单词。接下来N行每行一个单词,…

Android应用开发—RecyclerView绘制蒙层

背景&#xff1a;如何在跨越两个或两个以上的item绘制一个view&#xff0c;该view需要跟随recyclerView的滑动而整体移动。 Overridepublic void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDrawOver(c, parent, state);final View child …

排序_3

希尔排序:分组排序 是把记录按下标的一定增量分组&#xff0c;对每组使用直接插入排序算法排序&#xff1b; 随着增量逐渐减少&#xff0c;每组包含的关键词越来越多&#xff0c;当增量减至1时&#xff0c;整个文件恰被分成一组&#xff0c;算法便终止。 def shell_sort(array)…

face++算法工程实习生面试

2018-01-11 算法工程实习生 自动化工具链方面 面试的知识点非常仔细&#xff0c;十分检验基本功底 1.自我介绍 2.算法题&#xff0c;leetcode 第一题 两数之和 问python中数组和字典的查找时间复杂度 3.git git 4.linux 常用命令 cd - ,cd ,cd ~,cd / awk 读取倒数第一行&a…

IDEA中怎么设置黑色或白色背景?

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 开启软件Intellij IDEA&#xff0c;在编辑框上面找到最前端的File。 点击File&#xff0c;找到Setting&#xff0c;点击进入。 然后在…

大公司体制内创新的困境

周末在家&#xff0c;随手翻看了一点吴军老师的《浪潮之巅》这本书。去年这本书上市之后我从头到尾阅读了一遍&#xff0c;在《浪潮之巅》中吴军老师历数了IT行业公司的兴衰发展史&#xff0c;提出了一个令人印象深刻的“基因决定论”&#xff0c;即由于公司基因的影响&#xf…

java打印调用堆栈的方式

Log.d(TAG,Log.getStackTraceString(new Throwable()));

weblogic jprofile配置

前提&#xff1a; 1.安装好weblogic 2.安装好jprofile 非等待模式&#xff1a; export JAVA_OPTIONS"${JAVA_OPTIONS} -Dweblogic.threadpool.MinPoolSize100 -Dweblogic.threadpool.MaxPoolSize1000 -Djava.awt.headlesstrue -agentpath:/opt/jprofiler9/bin/linux-x64/l…

springboot/git学习资源记录

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 只是记录一下觉得不错的资源&#xff1a; springboot: http://bbs.itmayiedu.com/article/1508826968799 http://blog.720ui.com/tags…

音视频引擎研究

音视频包&#xff1a;http://ishare.iask.sina.com.cn/f/33851582.html 1、WebRTC目的 WebRTC&#xff08;Web Real-Time Communication&#xff09;项目的最终目的主要是让Web开发者能够基于浏览器&#xff08;Chrome\FireFox\...&#xff09;轻易快捷开发出丰富的实时多媒体应…

我为什么“放弃”从事八年的嵌入式领域

由于嵌入式平台性能所限&#xff0c;以及相应的开发平台&#xff0c;工具&#xff0c;语言所限&#xff0c;导致很多前沿领域的软件工程理论&#xff0c;方法无法实施&#xff0c;有些跟不上时代的感觉。 ……

Linux命令替换字符串

:%s/str1/str2/ 用str2替换str1 转载于:https://www.cnblogs.com/haiyang21/p/10020503.html