首页 > 系统 > Android > 正文

Android开发之实现GridView支付宝九宫格

2019-10-24 20:34:22
字体:
来源:转载
供稿:网友

本文给大家介绍android开发之实现gridview支付宝九宫格,其原理是让每个item都设置成带有分割线的背景,在这不透漏太多内容,感兴趣的朋友请阅读全文

先给大家展示下关于仿支付宝钱包首页中带有分割线的gridview,俗称九宫格 的效果图,怎么样是不是和你想象的一样啊。在你的预料之中就继续访问以下代码内容吧。

Android开发之实现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. }  

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

 

 
  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中,这一步我想大家都应该都很清楚,这里就不多讲了,不懂的话,可以参考下面的项目代码。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表