Android 控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
关注finddreams:http://blog.csdn.net/finddreams/article/details/43486527
今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格。先上图,是你想要的效果么?如果是请继续往下看。
我们都知道ListView设置分割线是非常容易的,设置ListView的分割线颜色和宽度,只需要在布局中定义android:divider和android:dividerHeight属性即可。而GridView并没有这样的属性和方法,那我们改如何来做呢?
博主在做这个效果之前,也参考了其他的一些方案,比如说定义一个自定义的GridView,然后在dispatchDraw()方法中在每个item的四周加上一条分割线,这是需要靠算法来实现的,最后这种方法实现的效果并不理想,会出现有些item中没有加上分割线,很难达到我们想要的这种效果。
其实实现这种效果并不难,原理就是让每个item都设置成带有分割线的背景,这样就很容易实现了。
首先我们来写布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ScrollView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:fillViewport="true"
- android:scrollbars="none" >
- <com.finddreams.alipay.MyGridView
- android:id="@+id/gridview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:horizontalSpacing="0.0dip"
- android:listSelector="@null"
- android:numColumns="3"
- android:scrollbars="none"
- android:stretchMode="columnWidth"
- android:verticalSpacing="0.0dip" />
- </ScrollView>
- </LinearLayout>
因为有时候我们的Gridview中的item可能比较多,为了放得下,一般都会用一个ScrollView来嵌套起来。这时就会出现一个常见的问题,我们在开发中经常会碰到,就是当ListView或者GridView被嵌套在ScrollView中时,发现只会显示第一行的数据,后面的数据就不会显示了。至于产生这个问题的原因,可能是因为Gridview和ListView都是可以根据子item的宽高来显示大小的,但是一旦嵌套到ScrollView中就可以上下滑动,于是系统就不能确定到底该画多大,所以才会产生这样的问题。
这个问题的解决方法在网上很多,一般百度一下就能查到,下面是GridView的解决方法:
- public class MyGridView extends GridView {
- public MyGridView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyGridView(Context context) {
- super(context);
- }
- public MyGridView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- @Override
- public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
- MeasureSpec.AT_MOST);
- super.onMeasure(widthMeasureSpec, expandSpec);
- }
- }
接下来,我们就定义一个带分割线的选择器,具体代码是:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true"><shape android:shape="rectangle">
- <stroke android:width="1.0px" android:color="@color/line" />
- <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
- </shape></item>
- <item android:state_focused="true"><shape android:shape="rectangle">
- <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
- <stroke android:width="1.0px" android:color="@color/line" />
- </shape></item>
- <item><shape android:shape="rectangle">
- <gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" />
- <stroke android:width="1.0px" android:color="@color/line" />
- </shape></item>
- </selector>
定义一个selector,在里面设置一个形状为矩形rectangle,设置这个矩形的stroke描边属性的颜色为分割线的颜色,然后在不同的state的item中设置不同的gradient渐变属性,从而实现在单个item在被点击选中时的效果。
接着就是给我们GridView的item布局中加上背景了:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="0.0dip"
- android:background="@color/griditems_bg" >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_centerInParent="true"
- android:background="@drawable/bg_gv"
- android:padding="12.0dip" >
- <ImageView
- android:id="@+id/iv_item"
- android:layout_width="58.0dip"
- android:layout_height="58.0dip"
- android:layout_centerHorizontal="true"
- android:contentDescription="@string/app_name" />
- <TextView
- android:id="@+id/tv_item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/iv_item"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="5.0dip"
- android:maxLines="1"
- android:textColor="@color/commo_text_color"
- android:textSize="14.0sp" />
- </RelativeLayout>
- </RelativeLayout>
到这里,就要开始写代码了,定义一个Adapter,把数据填充到GridView中,这一步我想大家都应该都很清楚,这里就不多讲了,不懂的话,可以参考下面的项目代码。
项目链接:http://download.csdn.net/detail/finddreams/8423263 给有需要的朋友!
Android 控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现相关推荐
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格.先上图,是你想要的效果么?如果是请继续往下看. 我们都知道ListView设置分割线是非常容易的,设置ListView的分割 ...
- 仿支付宝钱包:带分割线的GridView
需求: 本文记录了我尝试实现支付宝钱包样式带分割线GridView的过程.首先看一下高大上的支付宝钱包首页: 这里画红框的部分,给人的直观感觉就是一个GridView .当然,这里很可能是支付宝同学自 ...
- android 仿旅游日历控件_Android实现仿魅族日历首页功能
flyme5.0增加了很多优美的动画和交互,界面也变得相当精致.我手头现在就用着魅族MX5,感觉还不错哇!经常会打开它的日历看计划等,感觉它首页的滑动效果还不错,就试着实现一把. 效果分析 1 该首页 ...
- android 微信创建群ui,Android控件:高仿微信主UI
高仿微信主UI 之前在Android组件:Fragment切换后保存状态 一文中讲到了Fragment切换后,是如何保存原来的状态的,最重要的就是用add方法取代现在各种教程常见的replace方法. ...
- 自定义控件android.r,Android控件架构与自定义控件
前言 最近在开发的路上越走越远了,每天在看各位大神公众号更新内容是自定义View的时候,一些小的内容有点模具,决定回过头来温习一下过往的内容.此篇也是根据android群英传来总结的一篇文章. 1 A ...
- Android 控件 RecyclerView 看这篇就够了
[Android 控件 RecyclerView] 概述 RecyclerView是什么 从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传 ...
- Android 控件 RecyclerView
[Android 控件 RecyclerView] 概述 RecyclerView是什么 从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传 ...
- Android控件架构与自定义控件
引言 最近在开发的路上越走越远了,每天在看各位大神公众号更新内容是自定义View的时候,一些小的内容有点模具,决定回过头来温习一下过往的内容.此篇也是根据android群英传来总结的一篇文章. 1 A ...
- android 代码控件框高,Android控件_TextView(显示文本框控件)
一.TextView控件的常用属性 1.android:id--控件的id 2.android:layout_width--设置控件的宽度 wrap_content(包裹实际文本内容) fill_pa ...
最新文章
- 方便的boost_python
- java运行原理_Java程序的加载与运行原理详解
- matlab读取pdb文件,使用BioPython读取.pdb文件的整个目录
- matlab编写数字基带信号程序,数字基带信号的系统仿真与设计matlab程序
- KubeCon+CloudNativeCon首登中国,时速云受邀发表主题演讲
- C#动态生成Word文档并填充数据(一)
- C程序设计语言现代方法01:C语言概述
- BZOJ1090[SCOI2003] 字符串折叠
- 2021高考倒计时HTML源码,2021高考倒计时
- 淘宝商城 入住费用
- SAP采购订单控制价格是否可以修改增强 LV69AFZZ
- latex中lstlisting使用
- 宁德时代打响增长保卫战
- Arrays.stream()
- 跟益达学Solr5之使用MMSeg4J分词器
- MySQL常用函数大全(面试篇)
- 2022-2027年中国记忆绵床垫行业发展前景及投资战略咨询报告
- 互联网采集数据有哪几种常见的方法?
- DZ论坛全自动挂机回帖助手2015.10.25实用版
- 一个功能强大的画图板(二)
热门文章
- SPARROW-JS 从0开始写 0依赖,原生JS框架
- 【Android】Fragment懒加载和ViewPager的坑
- 从DataSet 导出到Excel(是DataSet中的每个DataTable对应每个Sheet)
- python爬网易歌单_Python爬取网易云歌单
- rt2870 linux,『求助』RaLink雷凌RT2870 无线网卡怎样安装驱动?
- PHP中css中文意思是,css中font-family是什么意思
- 火车头采集器采集图片文章详细 教程
- 用卫星地图告诉你新疆到底有多大
- android 短信 代码错误,android – Firebase手机身份验证错误:短信代码已过期
- uniGUI获取设备信息