Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實現仿支付寶九宮格 GridView(帶分割線)

編輯:關於 Android 程式設計

先欣賞一下仿支付寶錢包首頁、帶分割線的 GridView(俗稱九宮格)效果圖,是不是和你想象中的一樣呢?如果你的預料沒錯,就繼續看下面的實現思路與代碼吧。

我們都知道,ListView 設置分割線非常容易,只需在布局中定義 android:divider 和 android:dividerHeight 屬性即可;而 GridView 並沒有提供這樣的屬性和方法,那該怎麼辦呢?

在動手之前,筆者曾參考過一些方案,比如自定義一個 GridView,在 dispatchDraw() 方法中為每個 item 四周畫上分割線——這需要靠算法實現,最終效果並不理想,有些 item 會漏畫分割線,很難達到預期。

其實實現起來並不難,原理就是讓每個 item 都帶上分割線背景,這樣就水到渠成了。

一、編寫布局

 <LinearLayout xmlns:android="https://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 的處理方式——重寫 onMeasure:

 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="https://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 中設置形狀為矩形,並為矩形設置 stroke 描邊(顏色即分割線顏色),再在不同 state 的 item 中設置不同的 gradient,就能實現單個 item 被點擊選中時的效果。

三、為 item 布局添加背景

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://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 中即可。這一步大家都比較熟悉,這裡不再贅述,可參考完整項目代碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved