欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)之實(shí)現(xiàn)GridView支付寶九宮格

 更新時間:2015年11月16日 16:03:48   作者:一葉飄舟  
本文給大家介紹android開發(fā)之實(shí)現(xiàn)gridview支付寶九宮格,其原理是讓每個item都設(shè)置成帶有分割線的背景,在這不透漏太多內(nèi)容,感興趣的朋友請閱讀全文

先給大家展示下關(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)該都很清楚,這里就不多講了,不懂的話,可以參考下面的項目代碼。

相關(guān)文章

  • Android廣播事件流程與廣播ANR原理深入刨析

    Android廣播事件流程與廣播ANR原理深入刨析

    這篇文章主要介紹了Android廣播事件流程與廣播ANR原理,ANR應(yīng)用程序未響應(yīng),當(dāng)主線程被阻塞時,就會彈出如下彈窗,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可任意參考一下
    2022-10-10
  • 從零開始學(xué)android小示例程序

    從零開始學(xué)android小示例程序

    這篇文章主要介紹了一個學(xué)習(xí)android開發(fā)的小示例程序,需要的朋友可以參考下
    2014-02-02
  • Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解

    Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解

    這篇文章主要介紹了Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Kotlin掛起函數(shù)的詳細(xì)介紹

    Kotlin掛起函數(shù)的詳細(xì)介紹

    掛起函數(shù)用狀態(tài)機(jī)以掛起點(diǎn)將協(xié)程的運(yùn)算邏輯拆分成不同的片段,每次執(zhí)行協(xié)程運(yùn)行不同的邏輯片段,由此可以知道協(xié)程是運(yùn)行在線程中的,線程的并發(fā)處理方式也可以用在協(xié)程上
    2022-09-09
  • Android中g(shù)ravity與layout_gravity的使用區(qū)別分析

    Android中g(shù)ravity與layout_gravity的使用區(qū)別分析

    本篇文章介紹了,在Android中g(shù)ravity與layout_gravity的使用區(qū)別分析。需要的朋友參考下
    2013-04-04
  • Android高仿2048小游戲?qū)崿F(xiàn)代碼

    Android高仿2048小游戲?qū)崿F(xiàn)代碼

    這篇文章主要介紹了Android高仿2048小游戲?qū)崿F(xiàn)代碼的相關(guān)資料,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • Android QQ登錄界面繪制代碼

    Android QQ登錄界面繪制代碼

    這篇文章主要為大家詳細(xì)介紹了Android QQ登錄界面繪制代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android快速分析apk工具aapt的使用教程

    Android快速分析apk工具aapt的使用教程

    這篇文章主要介紹了Android快速分析apk工具aapt的使用教程,本文講解了什么是aapt、主要用法、使用aapt、查看apk的基本信息、查看基本信息、查看應(yīng)用權(quán)限等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Android 如何獲取設(shè)備唯一標(biāo)識

    Android 如何獲取設(shè)備唯一標(biāo)識

    這篇文章主要介紹了Android 如何獲取設(shè)備唯一標(biāo)識,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android中的HTextView庫實(shí)現(xiàn)TextView動畫效果

    Android中的HTextView庫實(shí)現(xiàn)TextView動畫效果

    HTextView是一個用來給TextView里的文字做各種轉(zhuǎn)換動畫的開源庫,不僅提供了多種動畫選擇,而且還有重復(fù)字符的位移動畫,雖然并沒有多么復(fù)雜,但是它使用的這些典型的設(shè)計模式以及各種動畫的實(shí)現(xiàn)確實(shí)可以從中讓我們學(xué)到不少知識
    2023-12-12

最新評論