最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)該都很清楚,這里就不多講了,不懂的話,可以參考下面的項(xiàng)目代碼。

相關(guān)文章

最新評論

页游| 海阳市| 潢川县| 临洮县| 新巴尔虎右旗| 星子县| 周至县| 开远市| 彰化县| 芜湖市| 县级市| 株洲市| 红原县| 海盐县| 兴宁市| 砀山县| 德令哈市| 天峨县| 安乡县| 株洲市| 和平区| 莱芜市| 泰宁县| 开平市| 车致| 蓬莱市| 潼关县| 建湖县| 突泉县| 内黄县| 时尚| 常熟市| 深圳市| 杭锦旗| 孟津县| 和静县| 平乡县| 昭苏县| 原平市| 武平县| 石家庄市|