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都设置成带有分割线的背景,这样就很容易实现了。

首先我们来写布局:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <ScrollView
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:fillViewport="true"
  9. android:scrollbars="none" >
  10. <com.finddreams.alipay.MyGridView
  11. android:id="@+id/gridview"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:horizontalSpacing="0.0dip"
  15. android:listSelector="@null"
  16. android:numColumns="3"
  17. android:scrollbars="none"
  18. android:stretchMode="columnWidth"
  19. android:verticalSpacing="0.0dip" />
  20. </ScrollView>
  21. </LinearLayout>

因为有时候我们的Gridview中的item可能比较多,为了放得下,一般都会用一个ScrollView来嵌套起来。这时就会出现一个常见的问题,我们在开发中经常会碰到,就是当ListView或者GridView被嵌套在ScrollView中时,发现只会显示第一行的数据,后面的数据就不会显示了。至于产生这个问题的原因,可能是因为Gridview和ListView都是可以根据子item的宽高来显示大小的,但是一旦嵌套到ScrollView中就可以上下滑动,于是系统就不能确定到底该画多大,所以才会产生这样的问题。

这个问题的解决方法在网上很多,一般百度一下就能查到,下面是GridView的解决方法:

[java] view plaincopy
  1. public class MyGridView extends GridView {
  2. public MyGridView(Context context, AttributeSet attrs) {
  3. super(context, attrs);
  4. }
  5. public MyGridView(Context context) {
  6. super(context);
  7. }
  8. public MyGridView(Context context, AttributeSet attrs, int defStyle) {
  9. super(context, attrs, defStyle);
  10. }
  11. @Override
  12. public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  13. int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
  14. MeasureSpec.AT_MOST);
  15. super.onMeasure(widthMeasureSpec, expandSpec);
  16. }
  17. }

接下来,我们就定义一个带分割线的选择器,具体代码是:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true"><shape android:shape="rectangle">
  4. <stroke android:width="1.0px" android:color="@color/line" />
  5. <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
  6. </shape></item>
  7. <item android:state_focused="true"><shape android:shape="rectangle">
  8. <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
  9. <stroke android:width="1.0px" android:color="@color/line" />
  10. </shape></item>
  11. <item><shape android:shape="rectangle">
  12. <gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" />
  13. <stroke android:width="1.0px" android:color="@color/line" />
  14. </shape></item>
  15. </selector>

定义一个selector,在里面设置一个形状为矩形rectangle,设置这个矩形的stroke描边属性的颜色为分割线的颜色,然后在不同的state的item中设置不同的gradient渐变属性,从而实现在单个item在被点击选中时的效果。

接着就是给我们GridView的item布局中加上背景了:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:layout_margin="0.0dip"
  6. android:background="@color/griditems_bg" >
  7. <RelativeLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_centerInParent="true"
  11. android:background="@drawable/bg_gv"
  12. android:padding="12.0dip" >
  13. <ImageView
  14. android:id="@+id/iv_item"
  15. android:layout_width="58.0dip"
  16. android:layout_height="58.0dip"
  17. android:layout_centerHorizontal="true"
  18. android:contentDescription="@string/app_name" />
  19. <TextView
  20. android:id="@+id/tv_item"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@id/iv_item"
  24. android:layout_centerHorizontal="true"
  25. android:layout_marginTop="5.0dip"
  26. android:maxLines="1"
  27. android:textColor="@color/commo_text_color"
  28. android:textSize="14.0sp" />
  29. </RelativeLayout>
  30. </RelativeLayout>

到这里,就要开始写代码了,定义一个Adapter,把数据填充到GridView中,这一步我想大家都应该都很清楚,这里就不多讲了,不懂的话,可以参考下面的项目代码。

项目链接:http://download.csdn.net/detail/finddreams/8423263    给有需要的朋友!

Android 控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现相关推荐

  1. Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现

    今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格.先上图,是你想要的效果么?如果是请继续往下看. 我们都知道ListView设置分割线是非常容易的,设置ListView的分割 ...

  2. 仿支付宝钱包:带分割线的GridView

    需求: 本文记录了我尝试实现支付宝钱包样式带分割线GridView的过程.首先看一下高大上的支付宝钱包首页: 这里画红框的部分,给人的直观感觉就是一个GridView .当然,这里很可能是支付宝同学自 ...

  3. android 仿旅游日历控件_Android实现仿魅族日历首页功能

    flyme5.0增加了很多优美的动画和交互,界面也变得相当精致.我手头现在就用着魅族MX5,感觉还不错哇!经常会打开它的日历看计划等,感觉它首页的滑动效果还不错,就试着实现一把. 效果分析 1 该首页 ...

  4. android 微信创建群ui,Android控件:高仿微信主UI

    高仿微信主UI 之前在Android组件:Fragment切换后保存状态 一文中讲到了Fragment切换后,是如何保存原来的状态的,最重要的就是用add方法取代现在各种教程常见的replace方法. ...

  5. 自定义控件android.r,Android控件架构与自定义控件

    前言 最近在开发的路上越走越远了,每天在看各位大神公众号更新内容是自定义View的时候,一些小的内容有点模具,决定回过头来温习一下过往的内容.此篇也是根据android群英传来总结的一篇文章. 1 A ...

  6. Android 控件 RecyclerView 看这篇就够了

    [Android 控件 RecyclerView] 概述 RecyclerView是什么 从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传 ...

  7. Android 控件 RecyclerView

    [Android 控件 RecyclerView] 概述 RecyclerView是什么 从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传 ...

  8. Android控件架构与自定义控件

    引言 最近在开发的路上越走越远了,每天在看各位大神公众号更新内容是自定义View的时候,一些小的内容有点模具,决定回过头来温习一下过往的内容.此篇也是根据android群英传来总结的一篇文章. 1 A ...

  9. android 代码控件框高,Android控件_TextView(显示文本框控件)

    一.TextView控件的常用属性 1.android:id--控件的id 2.android:layout_width--设置控件的宽度 wrap_content(包裹实际文本内容) fill_pa ...

最新文章

  1. 方便的boost_python
  2. java运行原理_Java程序的加载与运行原理详解
  3. matlab读取pdb文件,使用BioPython读取.pdb文件的整个目录
  4. matlab编写数字基带信号程序,数字基带信号的系统仿真与设计matlab程序
  5. KubeCon+CloudNativeCon首登中国,时速云受邀发表主题演讲
  6. C#动态生成Word文档并填充数据(一)
  7. C程序设计语言现代方法01:C语言概述
  8. BZOJ1090[SCOI2003] 字符串折叠
  9. 2021高考倒计时HTML源码,2021高考倒计时
  10. 淘宝商城 入住费用
  11. SAP采购订单控制价格是否可以修改增强 LV69AFZZ
  12. latex中lstlisting使用
  13. 宁德时代打响增长保卫战
  14. Arrays.stream()
  15. 跟益达学Solr5之使用MMSeg4J分词器
  16. MySQL常用函数大全(面试篇)
  17. 2022-2027年中国记忆绵床垫行业发展前景及投资战略咨询报告
  18. 互联网采集数据有哪几种常见的方法?
  19. DZ论坛全自动挂机回帖助手2015.10.25实用版
  20. 一个功能强大的画图板(二)

热门文章

  1. SPARROW-JS 从0开始写 0依赖,原生JS框架
  2. 【Android】Fragment懒加载和ViewPager的坑
  3. 从DataSet 导出到Excel(是DataSet中的每个DataTable对应每个Sheet)
  4. python爬网易歌单_Python爬取网易云歌单
  5. rt2870 linux,『求助』RaLink雷凌RT2870 无线网卡怎样安装驱动?
  6. PHP中css中文意思是,css中font-family是什么意思
  7. 火车头采集器采集图片文章详细 教程
  8. 用卫星地图告诉你新疆到底有多大
  9. android 短信 代码错误,android – Firebase手机身份验证错误:短信代码已过期
  10. uniGUI获取设备信息