帶有分割線的GridView九宮格的完美實現

 今天我們來模仿一下支付寶錢包首頁中帶有分割線的GridView,俗稱九宮格。先上圖,是你想要的效果麼?如果是請繼續往下看。

                                                         

          我們都知道ListView設置分割線是非常容易的,設置ListView的分割線顏色和寬度,只需要在佈局中定義android:dividerandroid: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.      
  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 plaincopy
  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 plaincopy
  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 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.   
  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中,這一步我想大家都應該都很清楚,這裏就不多講了,不懂的話,可以參考下面的項目代碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章