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

本文主要是介绍Android 控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



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


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

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

                                                         

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

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

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

       首先我们来写布局: 

[html]  view plain copy
  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.      
  6.      <ScrollView  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:fillViewport="true"  
  10.         android:scrollbars="none" >  
  11.   
  12.         <com.finddreams.alipay.MyGridView  
  13.             android:id="@+id/gridview"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:horizontalSpacing="0.0dip"  
  17.             android:listSelector="@null"  
  18.             android:numColumns="3"  
  19.             android:scrollbars="none"  
  20.             android:stretchMode="columnWidth"  
  21.             android:verticalSpacing="0.0dip" />  
  22.     </ScrollView>  
  23.   
  24. </LinearLayout>  

 

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

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

 

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

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

 

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

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

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

[html]  view plain copy
  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.   
  8.     <RelativeLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_centerInParent="true"  
  12.         android:background="@drawable/bg_gv"  
  13.         android:padding="12.0dip" >  
  14.   
  15.         <ImageView  
  16.             android:id="@+id/iv_item"  
  17.             android:layout_width="58.0dip"  
  18.             android:layout_height="58.0dip"  
  19.             android:layout_centerHorizontal="true"  
  20.             android:contentDescription="@string/app_name" />  
  21.   
  22.         <TextView  
  23.             android:id="@+id/tv_item"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:layout_below="@id/iv_item"  
  27.             android:layout_centerHorizontal="true"  
  28.             android:layout_marginTop="5.0dip"  
  29.             android:maxLines="1"  
  30.             android:textColor="@color/commo_text_color"  
  31.             android:textSize="14.0sp" />  
  32.     </RelativeLayout>  
  33.   
  34. </RelativeLayout>  


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

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


这篇关于Android 控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994674

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依