Android RecyclerView 实现支付宝首页效果

虽然我本人不喜欢支付宝的,但是这个网格本身其实还是不错的,项目更新中更改了一个布局为网格模式,类似支付宝.(估计是产品抄袭的=.=,我不管设计,只管实现就好.)

RecyclerView的功能已经模块化了,如下所示:

类名 描述
RecyclerView.Adapter 托管数据集合,为每个Item创建视图
RecyclerView.ViewHolder 承载Item视图的子视图
RecyclerView.LayoutManager 负责Item视图的布局
RecyclerView.ItemDecoration 为每个Item视图添加子视图,在Demo中被用来绘制Divider
RecyclerView.ItemAnimator 负责添加、删除数据时的动画效果

今天的重点是RecyclerView.ItemDecoration毕竟是来定义分隔线的,那就开始画吧 =.=

首先是模拟数据

public List<GridTabEntity> getData() {
List<GridTabEntity> data=new ArrayList<GridTabEntity>();
TypedArray typedArray = getResources().obtainTypedArray(R.array.image_home_arr);//这里是图表
String[] nameStr=new String[]{
"提现",
"自助上单",
"商品管理",
"全民营销",
"消费统计",
"评价管理",
"经营管理"
};
for (int i = 0; i < nameStr.length; i++) {
data.add(new GridTabEntity(nameStr[i],false,0,typedArray.getResourceId(i,0)));
}
return data;
}

addItemDecoration 定制分隔线

mGridTab = ((RecyclerView) findViewById(R.id.re_grid));

        mGridTab.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false));
//dp转px
final int offset = DisplayUtil.dp2px(this, 1.3f);
//这里是开始,定制分隔线
mGridTab.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView
.State state) {
super.getItemOffsets(outRect, view, parent, state);
int childLayoutPosition = parent.getChildLayoutPosition(view);
if (childLayoutPosition%3!=0){
outRect.right=offset/2;
outRect.bottom=offset/2;
}else {
outRect.left=offset/2;
outRect.right=offset/2;
outRect.bottom=offset/2;
} } @Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//始化一个Paint
Paint paint = new Paint();
// paint.setColor(Color.parseColor("#B8B8B8"));
paint.setColor(Color.parseColor("#D8D8D8"));
paint.setStrokeWidth(offset); //获得RecyclerView中条目数量
int childCount = parent.getChildCount(); //遍历
for (int i = 0; i < childCount; i++) { //获得子View,也就是一个条目的View,准备给他画上边框
View childView = parent.getChildAt(i); //先获得子View的长宽,以及在屏幕上的位置
float x = childView.getX();
float y = childView.getY();
int width = childView.getWidth();
int height = childView.getHeight(); if (i % 3==2){
//h bottom
c.drawLine(x, y + height, x + width, y + height, paint);
continue;
}else {
c.drawLine(x + width, y, x + width, y + height, paint);
//h bottom
c.drawLine(x, y + height, x + width, y + height, paint);
continue;
}
// //根据这些点画条目的四周的线 h:水平 v:垂直
// //h top
// c.drawLine(x, y, x + width, y, paint);
// //v left
// c.drawLine(x, y, x, y + height, paint);
// //v right
// c.drawLine(x + width, y, x + width, y + height, paint);
// //h bottom
// c.drawLine(x, y + height, x + width, y + height, paint);
}
super.onDraw(c, parent, state);
}
});
GridTabAdapter mAdapter = new GridTabAdapter(data); mGridTab.setAdapter(mAdapter);

好吧,不要打我,将就着点看,这只是个demo,所以代码很乱,注释是后来加的,应该能看懂吧.

画线的时候注意下,不是所以的"方块"都需要画上下左右的,例如中间的那个方块如果四个方向都画那么必定会有线叠加在一起,那样很难的.(>﹏<。)~

效果:

这是demo效果:

这是实际的效果:

最新文章

  1. 【转载】推荐5款超实用的.NET性能分析工具
  2. atitit.j2ee 1.5 1.6 的不同跟 Servlet 3.0新特性总结
  3. centos安装环境准备工作
  4. linux下mysql命令
  5. 简谈HashMap、HashTable的区别
  6. Visual Studio 命中断点时 打印信息
  7. Bostonkey Simple calc
  8. BZOJ 2440: [中山市选2011]完全平方数( 二分答案 + 容斥原理 + 莫比乌斯函数 )
  9. PHP:phpMyAdmin如何解决本地导入文件(数据库)为2M的限制
  10. c# gdi+输出成不同mime类型的图片
  11. 使用canvas绘制时钟 (http://heeroluo.net/Article/Detail/95)
  12. Java IO(四)
  13. 牛客网练习赛7-D-无向图(bfs,链式前向星)
  14. apiCloud 选择图片,选择视频,压缩图片
  15. [CodeForces - 614D] D - Skills
  16. 基于NDK的Android防破解&amp; Android防破解 【转载】
  17. HTML|CSS总结与补充
  18. ecshop,大商创后台支付系统修改模板
  19. 美团DB数据同步到数据仓库的架构与实践
  20. python 数据类型详解(转)

热门文章

  1. 阿里巴巴直播内容风险防控中的AI力量
  2. 简谈百度坐标反转至WGS84的三种思路
  3. 札记:android手势识别,MotionEvent
  4. 【社工】NodeJS 应用仓库钓鱼
  5. IE10、IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题
  6. python开发环境搭建
  7. Hbase的伪分布式安装
  8. 【Machine Learning】机器学习及其基础概念简介
  9. linux应用调试技术之GDB和GDBServer
  10. 浅谈Java的throw与throws