Android開發(fā)之實(shí)現(xiàn)GridView支付寶九宮格
先給大家展示下關(guān)于仿支付寶錢包首頁中帶有分割線的gridview,俗稱九宮格 的效果圖,怎么樣是不是和你想象的一樣啊。在你的預(yù)料之中就繼續(xù)訪問以下代碼內(nèi)容吧。
我們都知道ListView設(shè)置分割線是非常容易的,設(shè)置ListView的分割線顏色和寬度,只需要在布局中定義android:divider和android:dividerHeight屬性即可。而GridView并沒有這樣的屬性和方法,那我們改如何來做呢?
腳本之家小編在做這個效果之前,也參考了其他的一些方案,比如說定義一個自定義的GridView,然后在dispatchDraw()方法中在每個item的四周加上一條分割線,這是需要靠算法來實(shí)現(xiàn)的,最后這種方法實(shí)現(xiàn)的效果并不理想,會出現(xiàn)有些item中沒有加上分割線,很難達(dá)到我們想要的這種效果。
其實(shí)實(shí)現(xiàn)這種效果并不難,原理就是讓每個item都設(shè)置成帶有分割線的背景,這樣就很容易實(shí)現(xiàn)了。
首先我們來寫布局:
<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>
因?yàn)橛袝r候我們的Gridview中的item可能比較多,為了放得下,一般都會用一個ScrollView來嵌套起來。這時就會出現(xiàn)一個常見的問題,我們在開發(fā)中經(jīng)常會碰到,就是當(dāng)ListView或者GridView被嵌套在ScrollView中時,發(fā)現(xiàn)只會顯示第一行的數(shù)據(jù),后面的數(shù)據(jù)就不會顯示了。至于產(chǎn)生這個問題的原因,可能是因?yàn)镚ridview和ListView都是可以根據(jù)子item的寬高來顯示大小的,但是一旦嵌套到ScrollView中就可以上下滑動,于是系統(tǒng)就不能確定到底該畫多大,所以才會產(chǎn)生這樣的問題。
這個問題的解決方法在網(wǎng)上很多,一般百度一下就能查到,下面是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,在里面設(shè)置一個形狀為矩形rectangle,設(shè)置這個矩形的stroke描邊屬性的顏色為分割線的顏色,然后在不同的state的item中設(shè)置不同的gradient漸變屬性,從而實(shí)現(xiàn)在單個item在被點(diǎn)擊選中時的效果。
接著就是給我們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,把數(shù)據(jù)填充到GridView中,這一步我想大家都應(yīng)該都很清楚,這里就不多講了,不懂的話,可以參考下面的項目代碼。
- Android 九宮格的實(shí)現(xiàn)方法
- android 九宮格滑動解鎖開機(jī)實(shí)例源碼學(xué)習(xí)
- Android實(shí)現(xiàn)九宮格(GridView中各項平分空間)的方法
- Android打造流暢九宮格抽獎活動效果
- 輕松實(shí)現(xiàn)Android自定義九宮格圖案解鎖
- Android實(shí)現(xiàn)九宮格解鎖
- Android實(shí)現(xiàn)九宮格橫向左右滑動
- Android編程之九宮格實(shí)現(xiàn)方法實(shí)例分析
- 輕松實(shí)現(xiàn)安卓(Android)九宮格解鎖
- Android實(shí)現(xiàn)圖片九宮格
相關(guān)文章
Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解
這篇文章主要介紹了Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解的相關(guān)資料,需要的朋友可以參考下2016-02-02Android中g(shù)ravity與layout_gravity的使用區(qū)別分析
本篇文章介紹了,在Android中g(shù)ravity與layout_gravity的使用區(qū)別分析。需要的朋友參考下2013-04-04Android高仿2048小游戲?qū)崿F(xiàn)代碼
這篇文章主要介紹了Android高仿2048小游戲?qū)崿F(xiàn)代碼的相關(guān)資料,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下2016-10-10Android中的HTextView庫實(shí)現(xiàn)TextView動畫效果
HTextView是一個用來給TextView里的文字做各種轉(zhuǎn)換動畫的開源庫,不僅提供了多種動畫選擇,而且還有重復(fù)字符的位移動畫,雖然并沒有多么復(fù)雜,但是它使用的這些典型的設(shè)計模式以及各種動畫的實(shí)現(xiàn)確實(shí)可以從中讓我們學(xué)到不少知識2023-12-12