2015-03-10 22:38 28419人阅读 评论(17) 收藏 举报
 分类:
Android UI(819)  Android开发(1568) 

关注finddreams:http://blog.csdn.net/finddreams/article/details/43486527

今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格。先上图,是你想要的效果么?如果是请继续往下看。

我们都知道ListView设置分割线是非常容易的,设置ListView的分割线颜色和宽度,只需要在布局中定义Android:divider和android:dividerHeight属性即可。而GridView并没有这样的属性和方法,那我们改如何来做呢?

博主在做这个效果之前,也参考了其他的一些方案,比如说定义一个自定义的GridView,然后在dispatchDraw()方法中在每个item的四周加上一条分割线,这是需要靠算法来实现的,最后这种方法实现的效果并不理想,会出现有些item中没有加上分割线,很难达到我们想要的这种效果。

其实实现这种效果并不难,原理就是让每个item都设置成带有分割线的背景,这样就很容易实现了。

首先我们来写布局:

  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的解决方法:

  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. }

接下来,我们就定义一个带分割线的选择器,drawable 单击右键 new -> Drawable resource file

具体代码是:

  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布局中加上背景了:

  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控件-ViewPager(仿微信引导界面)

    什么是ViewPager? ViewPager是安卓3.0之后提供的新特性,继承自ViewGroup,专门用以实现左右滑动切换View的效果. 如果想向下兼容就必须要android-support-v ...

  2. android控件库(2)-仿Google Camera 的对焦效果

    一直很喜欢Google Camera的自动对焦效果,今日闲来无事,自己做了个: 废话不多说,代码才是王道: package com.example.test.view; import com.exam ...

  3. Android 控件进阶修炼-仿360手机卫士波浪球进度控件

    技术:Android+java   概述 像360卫士的波浪球进度的效果,一般最常用的方法就是 画线的方式,先绘sin线或贝塞尔曲线,然后从左到右绘制竖线,然后再裁剪圆区域. 今天我这用图片bitma ...

  4. Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  5. Android控件Gridview实现多个menu模块,可添加可删除

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  6. Android控件介绍

    1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...

  7. Android控件RecyclerView的基本用法

    Android控件RecyclerView的基本用法 转 https://www.jianshu.com/p/e71a4b73098f   github: https://github.com/Cym ...

  8. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

  9. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

随机推荐

  1. 解决虚拟机vmware虚拟机安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题

    前言 虚拟机使用的是VMware Workstation,并且首次在虚拟机体验64 位系统.在新建好虚拟机,运行时候就出现了VMware Workstation 的提醒:此主机支持 Intel VT- ...

  2. GPU 显存释放

    我们在使用tensorflow 的时候, 有时候会在控制台终止掉正在运行的程序,但是有时候程序已经结束了,nvidia-smi也看到没有程序了,但是GPU的内存并没有释放,那么怎么解决该问题呢? 首先 ...

  3. Python 学习参考书目推荐

    Python 学习,参考书目推荐 前言 好的技术书籍可以帮助我们快速地成长,大部分人或多或少地受益于经典的技术书籍.在「Python开发者」微信公号后台,我们经常能收到让帮忙推荐书籍的消息.这类的问题 ...

  4. eclipse 开发 spring 、 springboot项目调试时一直跳转到 SilentExitExceptionHandler.exitCurrentThread 方法

    不想让 eclipse 调试时一直跳转到该方法 解决方法: Eclipse->[Preferences]->[Java]->[Debug]:去掉[Suspend execution ...

  5. HTTP报文头Accept和Content-Type总结

    1.Accept属于请求头, Content-Type属于实体头. Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http报头结构:通用报头|请求报头|实体报头 响应方的http报 ...

  6. 启动 angular-phonecat 项目时出现这玩意 。('The header content contains invalid characters');

    最近学习angular, 跟着视频做一个动作,启动 “ angular-phonecat ” 这个项目 敲入 “npm start ” 启动没有问题,但是 "http://localhost ...

  7. sql server中调用c#写的dll里的方法

    最近有一项目: 一超市管理系统单机版,运行在WIN2003+SQL2005上,每天超市关门都都会关电脑,现客户要新加功能,每天关门下班后回家可以上网查看超市管理系统的数据库里的相关数据,然后再做一些原 ...

  8. iOS 画平滑曲线的方法及取音频数据的方法

    源码:http://files.cnblogs.com/ios8/iOS%E5%BF%83%E7%94%B5%E5%9B%BEDemo.zip 取音频数据和画波形图的方法 ViewController ...

  9. 【Unity】第7章 输入控制

    分类:Unity.C#.VS2015 创建日期:2016-04-21 一.简介 Unity提供了-个非常易用和强大的用于处理输入信息的类:Input,利用该类可以处理鼠标.键盘.摇杆/方向盘/手柄等游 ...

  10. vue-cli项目配置图解

    配置vue项目机构步骤1.搭建环境:node.npm2.安装vue脚手架工具:npm install -g vue-cli3.初始化项目:vue init webpack vuedemo(“vuede ...