在實現(xiàn)類似通信錄,等帶有兩層或多層組織架構(gòu)的列表功能時,會使用到ExpandableListView,他是ListView的子類,使用方式也和ListView大同小異,這里做一個總結(jié)。

關(guān)鍵類:
1、SimpleExpandableListAdapter
2、SimpleCursorTreeAdapter
3、BaseExpandableListAdapter
4、ExpandableListActivity
SimpleExpandableListAdapter
構(gòu)造方法:
SimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData, //組數(shù)據(jù)
int groupLayout,//組數(shù)據(jù)使用的布局文件
String[] groupFrom, //組數(shù)據(jù)所使用的key數(shù)組
int[] groupTo,//組數(shù)據(jù)放置的控件id,例如TextView
List<? extends List<? extends Map<String,?>>> childData,//子元素數(shù)據(jù)
int childLayout, //子元素使用的布局文件
String[] childFrom, //子元素數(shù)據(jù)所使用的key數(shù)組
int[] childTo)//子元素數(shù)據(jù)放置的控件id,例如TextView
例如:生成一條組數(shù)據(jù),組名為“group1”
Map<String, String> map1 = new HashMap<>();
map1.put("group", "group1");//其中"group"只是一個索引key
集合保存所有的組數(shù)據(jù)
List<Map<String, String>> groups = new ArrayList<>();
保存一條子元素數(shù)據(jù),和組數(shù)據(jù)一樣
Map<String, String> cMap1 = new HashMap<>();
cMap1.put("child","child1_data1");
child1.add(cMap1);//添加到集合
保存一個組的子元素數(shù)據(jù)
List<Map<String, String>> child1 = new ArrayList<>();
保存所有組的子元素數(shù)據(jù)
List<List<Map<String, String>>> childs = new ArrayList<>();
構(gòu)造方法
SimpleExpandableListAdapter adapter1 = new SimpleExpandableListAdapter(this,
groups, R.layout.expandable_group, new String[]{"group"}, new int[]{R.id.text_group},
childs, R.layout.expandable_child, new String[]{"child"}, new int[]{R.id.text_child});
BaseExpandableListAdapter
繼承BaseExpandableListAdapter實現(xiàn)自己的Adapter:
public class MyAdapter extends BaseExpandableListAdapter{
String[] groups = new String[]{"Group_111","Group_222","Group_333"};
String[][] childs = new String[][]{
{"g1_c1","g1_c2","g1_c3"},
{"g2_c1","g2_c2","g2_c3"},
{"g3_c1","g3_c2","g3_c3"}};
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return childs[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;//這里直接返回組元素下標
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;//這里直接返回第二層元素下標
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = View.inflate(MainActivity.this, R.layout.expandable_group, null);
TextView text = (TextView)view.findViewById(R.id.text_group);
text.setText(groups[groupPosition]);
return view;//實際項目中應該考慮convertView的重用
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = View.inflate(MainActivity.this, R.layout.expandable_child, null);
TextView text = (TextView)view.findViewById(R.id.text_child);
text.setText(childs[groupPosition][childPosition]);
return view;
}
@Override//組合子元素是否擁有穩(wěn)定的id,底層的數(shù)據(jù)集改表是否影響該id
public boolean hasStableIds() {
return false;
}
@Override//子元素是否可選中,有點擊反饋
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}ExpandableListActivity和ListActivity類似
寫自己的Activity繼承自該類
該類自帶一個ExpandableListView。id為:“@android:id/list”(或者代碼中為“l(fā)ist”)
可以有一個id為:“@android:id/empty”布局,作為adapter沒數(shù)據(jù)時的替代布局
通常寫自己的布局并滿足上述條件,并加以擴展實現(xiàn)自己的需求
SimpleCursorTreeAdapter
不同于SimpleExpandableListAdapter數(shù)據(jù)源來自List<Map<?,?>>,SimpleCursorTreeAdapter數(shù)據(jù)源來自訪問數(shù)據(jù)庫返回的Cursor。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文標題:ExpandableListView總結(jié)-創(chuàng)新互聯(lián)
標題URL:http://chinadenli.net/article28/cecicp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站制作、響應式網(wǎng)站、關(guān)鍵詞優(yōu)化、Google、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容