ExpandableListView是一個垂直滾動顯示兩級列表項的視圖,與ListView不同的是,它可以有兩層:每一層都能夠被獨立的展開并顯示其子項。這些子項來自于與該視圖關(guān)聯(lián)的ExpandableListAdapter。
每一個可以擴展的列表項的旁邊都有一個指示符(箭頭)用來說明該列表項目前的狀態(tài)(這些狀態(tài)一般是已經(jīng)擴展開的列表項,還沒有擴展開的列表項,子列表項和最后一個子列表項)??梢允褂梅椒ǎ簊etChildIndicator(Drawable),setGroupIndicator(Drawable)(或者相應(yīng)的xml屬性設(shè)置)去設(shè)置這些指示符的樣式。當然也可以使用默認的指示符:android.R.layout.simple_expandable_list_item_1,android.R.layout.simple_expandable_list_item_2
ExpandableListAdapter adapter = new BaseExpandableListAdapter();
BaseExpandableListAdapter的主要重載方法如下:
public abstract Object getChild (int groupPosition, int childPosition)
//取得與指定分組、指定子項目關(guān)聯(lián)的數(shù)據(jù).
參數(shù):
groupPosition 包含子視圖的分組的位置.
childPosition 指定的分組中的子視圖的位置.(子視圖中的子視圖的位置,上面的那個子視圖衍化成了一個組)
返回:
與子視圖關(guān)聯(lián)的數(shù)據(jù).
public abstract long getChildId (int groupPosition, intchildPosition)
//取得給定分組中給定子視圖的ID. 該組ID必須在組中是唯一的.必須不同于其他所有ID(分組及子項目的ID).
參數(shù):
groupPosition 包含子視圖的分組的位置,
childPosition 要取得ID的指定的分組中的子視圖的位置.
返回:
與子視圖關(guān)聯(lián)的ID.
public abstract int getChildrenCount (int groupPosition)
//取得指定分組的子元素數(shù).
參數(shù):
groupPosition 要取得子元素個數(shù)的分組位置.
返回:
指定分組的子元素個數(shù).
public abstract View getChildView (int groupPosition, intchildPosition, boolean isLastChild, View convertView, ViewGroup parent)
//取得顯示給定分組給定子位置的數(shù)據(jù)用的視圖.
參數(shù):
groupPosition 包含要取得子視圖的分組位置.
childPosition 分組中子視圖(要返回的視圖)的位置.
isLastChild 該視圖是否為組中的最后一個視圖.
convertView 如果可能,重用舊的視圖對象.使用前你應(yīng)該保證視圖對象為非空,并且是否是合適的類型.如果該對象不能轉(zhuǎn)換為可以正確顯示數(shù)據(jù)的視圖,該方法就創(chuàng)建新視圖.不保證使用先前由 getChildView(int, int,boolean, View, ViewGroup)創(chuàng)建的視圖.
parent 該視圖最終從屬的父視圖.
返回:
指定位置相應(yīng)的子視圖.
public abstract Object getGroup (int groupPosition)
//取得與給定分組關(guān)聯(lián)的數(shù)據(jù).
參數(shù)
groupPosition 分組的位置.
返回:
指定分組的數(shù)據(jù).
public abstract int getGroupCount ()
//取得分組數(shù).
返回:
分組數(shù).
public abstract long getGroupId (int groupPosition)
//取得指定分組的ID.該組ID必須在組中是唯一的.必須不同于其他所有ID(分組及子項目的ID).
參數(shù):
groupPosition 要取得ID的分組位置.
返回:
與分組關(guān)聯(lián)的ID.
public abstract View getGroupView (int groupPosition, booleanisExpanded, View convertView, ViewGroup parent)
//取得用于顯示給定分組的視圖. 這個方法僅返回分組的視圖對象, 要想獲取子元素的視圖對象,就需要調(diào)用 getChildView(int, int, boolean, View, ViewGroup).
參數(shù):
groupPosition 決定返回哪個視圖的組位置 .
isExpanded 該組是展開狀態(tài)還是收起狀態(tài) .
convertView 如果可能,重用舊的視圖對象.使用前你應(yīng)該保證視圖對象為非空,并且是否是合適的類型.如果該對象不能轉(zhuǎn)換為可以正確顯示數(shù)據(jù)的視圖,該方法就創(chuàng)建新視圖.不保證使用先前由 getGroupView(int, boolean,View, ViewGroup)創(chuàng)建的視圖.
parent 該視圖最終從屬的父視圖.
返回:
指定位置相應(yīng)的組視圖.
public abstract boolean isChildSelectable (int groupPosition, intchildPosition)
//指定位置的子視圖是否可選擇.
參數(shù):
groupPosition 包含要取得子視圖的分組位置.
childPosition 分組中子視圖的位置.
返回:
是否子視圖可選擇.
通過覆寫上面的方法,就可以根據(jù)需求創(chuàng)建一個BaseExpandableListAdapter對象了。
再來說ExpandableListView:
在XML布局文件中,如果ExpandableListView上一級視圖的大小沒有嚴格定義的話,則不能對ExpandableListView的android:layout_height 屬性使用wrap_content值。 (例如,如果上一級視圖是ScrollView的話,則不應(yīng)該指定wrap_content的值,因為它可以是任意的長度。不過,如果ExpandableListView的上一級視圖有特定的大小的話,比如100像素,則可以使用wrap_content)
如果由于開發(fā)的時候粗心,對ExpandableListView指定wrap_content的值,則會報一個在SetContentView處的空指針錯誤。
在實際開發(fā)過程中,常常有不同的需求,比如每一個child需要不同的控件,每一個group或者child需要有圖標,圖標顯示需要不一樣,需要設(shè)置背景等各種各樣能夠讓我們的程序變得美觀的需求。
//舉例1:為ExpandableListView設(shè)置背景,并且默認展開第n組,n從0開始計數(shù),則只需要添加如下代碼:
myExpandableListView.setBackgroundResource(R.drawable.background);
myExpandableListView.expandGroup(0);
//舉例2:改變每個組前面的圖標,并且圖標樣式隨著合攏和展開不同,則只需要在res/drawable目錄下定義文件:Indicator.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_expanded="true" android:drawable="@drawable/right"/>
<item android:drawable="@drawable/down"></item>
</selector>
//在java文件中添加:myExpandableListView.setGroupIndicator(this.getResources().getDrawable(R.drawable.indicator));
其他屬性設(shè)置
android:childDivider
來分離子列表項的圖片或者是顏色。注:圖片不會完全顯示,分離子列表項的是一條直線
android:childIndicator
在子列表項旁邊顯示的指示符。注:可以是一個圖片
android:childIndicatorLeft
子列表項指示符的左邊約束位置。注:即從左端0位置開始計數(shù),比如,假設(shè)指示符是一個圖標,給定這個屬性值為3dip,則表示從左端起3dip開始顯示此圖標。
android:childIndicatorRight
子列表項指示符的右邊約束位置。注:表示右端到什么位置結(jié)束
android:groupIndicator
在組列表項旁邊顯示的指示符。注:可以是一個圖片。
android:indicatorLeft
組列表項指示器的左邊約束位置。注:表示左端從什么位置開始。
android:indicatorRight
組列表項指示器的右邊約束位置。注:表示右端到什么位置結(jié)束。