Android实现支付宝AR功能,Android RecyclerView 实现支付宝首页效果

Android RecyclerView 实现支付宝首页效果

[TOC]

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

类名

描述

RecyclerView.Adapter

托管数据集合,为每个Item创建视图

RecyclerView.ViewHolder

承载Item视图的子视图

RecyclerView.LayoutManager

负责Item视图的布局

RecyclerView.ItemDecoration

为每个Item视图添加子视图,在Demo中被用来绘制Divider

RecyclerView.ItemAnimator

负责添加、删除数据时的动画效果

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

类名

描述

RecyclerView.Adapter

托管数据集合,为每个Item创建视图

RecyclerView.ViewHolder

承载Item视图的子视图

RecyclerView.LayoutManager

负责Item视图的布局

RecyclerView.ItemDecoration

为每个Item视图添加子视图,在Demo中被用来绘制Divider

RecyclerView.ItemAnimator

负责添加、删除数据时的动画效果

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

首先是模拟数据

public List getData() {

List data=new ArrayList();

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效果:

4f8d1c809ec0

http://oahzrw11n.bkt.clouddn.com//pic/20160812/device-gridtab-demo.png

这是实际的效果:

4f8d1c809ec0

http://oahzrw11n.bkt.clouddn.com//pic/20160812/device-gridtab-true.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值