關注finddreams:http://blog.csdn.net/finddreams/article/details/43486527
今天我們來模仿一下支付寶錢包首頁中帶有分割線的GridView,俗稱九宮格。先上圖,是你想要的效果麼?如果是請繼續往下看。

我們都知道ListView設定分割線是非常容易的,設定ListView的分割線顔色和寬度,隻需要在布局中定義android:divider和android:dividerHeight屬性即可。而GridView并沒有這樣的屬性和方法,那我們改如何來做呢?
部落客在做這個效果之前,也參考了其他的一些方案,比如說定義一個自定義的GridView,然後在dispatchDraw()方法中在每個item的四周加上一條分割線,這是需要靠算法來實作的,最後這種方法實作的效果并不理想,會出現有些item中沒有加上分割線,很難達到我們想要的這種效果。
其實實作這種效果并不難,原理就是讓每個item都設定成帶有分割線的背景,這樣就很容易實作了。
首先我們來寫布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<com.finddreams.alipay.MyGridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="0.0dip"
android:listSelector="@null"
android:numColumns="3"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="0.0dip" />
</ScrollView>
</LinearLayout>
因為有時候我們的Gridview中的item可能比較多,為了放得下,一般都會用一個ScrollView來嵌套起來。這時就會出現一個常見的問題,我們在開發中經常會碰到,就是當ListView或者GridView被嵌套在ScrollView中時,發現隻會顯示第一行的資料,後面的資料就不會顯示了。至于産生這個問題的原因,可能是因為Gridview和ListView都是可以根據子item的寬高來顯示大小的,但是一旦嵌套到ScrollView中就可以上下滑動,于是系統就不能确定到底該畫多大,是以才會産生這樣的問題。
這個問題的解決方法在網上很多,一般百度一下就能查到,下面是GridView的解決方法:
public class MyGridView extends GridView {
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
接下來,我們就定義一個帶分割線的選擇器,具體代碼是:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape android:shape="rectangle">
<stroke android:width="1.0px" android:color="@color/line" />
<gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
</shape></item>
<item android:state_focused="true"><shape android:shape="rectangle">
<gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
<stroke android:width="1.0px" android:color="@color/line" />
</shape></item>
<item><shape android:shape="rectangle">
<gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" />
<stroke android:width="1.0px" android:color="@color/line" />
</shape></item>
</selector>
定義一個selector,在裡面設定一個形狀為矩形rectangle,設定這個矩形的stroke描邊屬性的顔色為分割線的顔色,然後在不同的state的item中設定不同的gradient漸變屬性,進而實作在單個item在被點選選中時的效果。
接着就是給我們GridView的item布局中加上背景了:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0.0dip"
android:background="@color/griditems_bg" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:background="@drawable/bg_gv"
android:padding="12.0dip" >
<ImageView
android:id="@+id/iv_item"
android:layout_width="58.0dip"
android:layout_height="58.0dip"
android:layout_centerHorizontal="true"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_item"
android:layout_centerHorizontal="true"
android:layout_marginTop="5.0dip"
android:maxLines="1"
android:textColor="@color/commo_text_color"
android:textSize="14.0sp" />
</RelativeLayout>
</RelativeLayout>
到這裡,就要開始寫代碼了,定義一個Adapter,把資料填充到GridView中,這一步我想大家都應該都很清楚,這裡就不多講了,不懂的話,可以參考下面的項目代碼。
項目連結:http://download.csdn.net/detail/finddreams/8423263 給有需要的朋友!