日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android数据适配-ExpandableListView

發(fā)布時(shí)間:2023/11/29 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android数据适配-ExpandableListView 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android中ListView的用法基本上學(xué)的時(shí)候都會(huì)使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去實(shí)現(xiàn),這次主要使用的ExpandableListView展示一種兩層的效果,ExpandableListView是android中可以實(shí)現(xiàn)下拉list的一個(gè)控件類(lèi)似于QQ那種我好友之后就是一排自己的好友,就是兩層效果,實(shí)現(xiàn)的話(huà)使用SimpleExpandableListAdapter即可。

布局文件

先看下效果

main中xml代碼:

1 2 3 4 5 6 7 8 9 10 11 <Button ??????android:onClick="test" ??????android:layout_width="fill_parent" ??????android:layout_height="wrap_content" ??????android:text="FlyElephant"?/> ??<ExpandableListView ??????android:id="@id/android:list" ??????android:layout_width="fill_parent" ??????android:layout_height="fill_parent" ??????android:drawSelectorOnTop="false"?/>

?定義一個(gè)省份的province.xml:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?xml version="1.0"?encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ????android:layout_width="match_parent" ????android:layout_height="match_parent" ????android:orientation="vertical"?> ????<TextView ????????android:id="@+id/list_provinceText" ????????android:layout_width="fill_parent" ????????android:layout_height="fill_parent" ????????android:paddingBottom="8px" ????????android:paddingLeft="30px" ????????android:paddingRight="5px" ????????android:paddingTop="8px" ????????android:textSize="20sp"?/> </LinearLayout>

定義了一個(gè)地區(qū)的child.xml:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version="1.0"?encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ????android:layout_width="match_parent" ????android:layout_height="match_parent" ????android:orientation="vertical"?> ????? ???????<TextView ????????android:id="@+id/child_text" ????????android:layout_width="fill_parent" ????????android:layout_height="fill_parent" ????????android:paddingBottom="8px" ????????android:paddingLeft="30px" ????????android:paddingRight="5px" ????????android:paddingTop="8px" ????????android:textSize="20sp"?/> ????? </LinearLayout>

?Demo實(shí)現(xiàn)

主要實(shí)現(xiàn)代碼,代碼中都已經(jīng)注釋,其中最主要的SimpleExpandableListAdapter中的參數(shù),這個(gè)參數(shù)太多,很容易弄錯(cuò),可以看下注釋或者API文檔:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 // 創(chuàng)建一級(jí)條目 ?List<Map<String, String>> provinces =?new?ArrayList<Map<String, String>>(); ?//創(chuàng)建兩個(gè)省份一級(jí)條目 ?Map<String, String> firstProvince=?new?HashMap<String, String>(); ?firstProvince.put("province",?"河南"); ?Map<String, String> secondProvince=?new?HashMap<String, String>(); ?secondProvince.put("province",?"北京"); ?provinces.add(firstProvince); ?provinces.add(secondProvince); ?// 創(chuàng)建一級(jí)條目下的的二級(jí)地區(qū)條目 ?List<Map<String, String>> childList1=?new?ArrayList<Map<String, String>>(); ?//同樣是在一級(jí)條目目錄下創(chuàng)建兩個(gè)對(duì)應(yīng)的二級(jí)條目目錄 ?Map<String, String> child1=?new?HashMap<String, String>(); ?child1.put("child",?"鄭州"); ?Map<String, String> child2 =?new?HashMap<String, String>(); ?child2.put("child",?"開(kāi)封"); ?childList1.add(child1); ?childList1.add(child2); ?//同上 ?List<Map<String, String>> childList2 =?new?ArrayList<Map<String, String>>(); ?Map<String, String> child3 =?new?HashMap<String, String>(); ?child3.put("child",?"海淀"); ?Map<String, String> child4 =?new?HashMap<String, String>(); ?child4.put("child",?"昌平"); ?childList2.add(child3); ?childList2.add(child4); ?// 將二級(jí)條目放在一個(gè)集合里,供顯示時(shí)使用 ?List<List<Map<String, String>>> childs =?new?ArrayList<List<Map<String, String>>>(); ?childs.add(childList1); ?childs.add(childList2); ?/** ??* 使用SimpleExpandableListAdapter顯示ExpandableListView ??* 參數(shù)1.上下文對(duì)象Context ??* 參數(shù)2.一級(jí)條目目錄集合 ??* 參數(shù)3.一級(jí)條目對(duì)應(yīng)的布局文件 ??* 參數(shù)4.fromto,就是map中的key,指定要顯示的對(duì)象 ??* 參數(shù)5.與參數(shù)4對(duì)應(yīng),指定要顯示在groups中的id ??* 參數(shù)6.二級(jí)條目目錄集合 ??* 參數(shù)7.二級(jí)條目對(duì)應(yīng)的布局文件 ??* 參數(shù)8.fromto,就是map中的key,指定要顯示的對(duì)象 ??* 參數(shù)9.與參數(shù)8對(duì)應(yīng),指定要顯示在childs中的id ??*/ ?SimpleExpandableListAdapter adapter =?new?SimpleExpandableListAdapter( ?????????this, provinces, R.layout.list_group,?new?String[] {?"province"?}, ?????????new?int[] { R.id.list_groupText }, childs, R.layout.child, ?????????new?String[] {?"child"?},?new?int[] { R.id.child_text }); ?setListAdapter(adapter);

這個(gè)mainActivity需要繼承ExpandableListActivity,當(dāng)然你可以設(shè)置其中的點(diǎn)擊事件,只要重寫(xiě)一下方法即可:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 /** ?* 設(shè)置哪個(gè)二級(jí)目錄被默認(rèn)選中 ?*/ @Override public?boolean?setSelectedChild(int?groupPosition,?int?childPosition, ????????boolean?shouldExpandGroup) { ????????//do something ????return?super.setSelectedChild(groupPosition, childPosition, ????????????shouldExpandGroup); } /** ?* 設(shè)置哪個(gè)一級(jí)目錄被默認(rèn)選中 ?*/ @Override public?void?setSelectedGroup(int?groupPosition) { ????//do something ????super.setSelectedGroup(groupPosition); } /** ?* 當(dāng)二級(jí)條目被點(diǎn)擊時(shí)響應(yīng) ?*/ @Override public?boolean?onChildClick(ExpandableListView parent, View v, ????????int?groupPosition,?int?childPosition,?long?id) { ????????//do something ????return?super.onChildClick(parent, v, groupPosition, childPosition, id); }

?效果如下:

?

上面這個(gè)例子寫(xiě)的有點(diǎn)單調(diào),其實(shí)第二個(gè)你子的布局直接是空的也行,例如定義一個(gè)images.xml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version="1.0"?encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ????android:layout_width="wrap_content" ????android:layout_height="wrap_content" ????android:orientation="horizontal"?> ????<ImageView ????????android:src="@drawable/open" ????????android:layout_width="20dp" ????????android:layout_height="20dp"?/> ????<TextView ????????android:id="@+id/txtName" ???????android:paddingLeft="10dp" ????????android:layout_width="wrap_content" ????????android:layout_height="wrap_content"?/> </LinearLayout>

然后定義一個(gè)items.xml

1 2 3 4 5 6 7 8 <?xml version="1.0"?encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" ????android:id="@+id/items" ????android:layout_width="wrap_content" ????android:layout_height="wrap_content"?> ????? </TextView>

 代碼調(diào)用:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public?class?MyExpandleActivity?extends?Activity { ????/** ?????* 實(shí)現(xiàn)可擴(kuò)展展開(kāi)列ExpandableListView的三種方式 ?????* 一是使用SimpleExpandableListAdpater將兩個(gè)List集合包裝成ExpandableListView 二是 ?????* 擴(kuò)展BaseExpandableListAdpter ?????* 三是使用simpleCursorTreeAdapter將Cursor中的數(shù)據(jù)包裝成SimpleCuroTreeAdapter ?????*/ ????private?String[] names = {?"騰訊",?"百度",?"阿里巴巴"?}; ????private?String[][] childnames = { {?"馬化騰",?"張小龍","社交"}, ????????????{?"李彥宏",?"馬東敏","搜索"?}, {?"馬云",?"陸兆禧","電商"?} }; ????private?ExpandableListView ep; ????@Override ????protected?void?onCreate(Bundle savedInstanceState) { ????????super.onCreate(savedInstanceState); ????????setContentView(R.layout.activity_my_expandle); ????????// 定義父列表項(xiàng)List數(shù)據(jù)集合 ????????List<Map<String, String>> group =?new?ArrayList<Map<String, String>>(); ????????// 定義子列表項(xiàng)List數(shù)據(jù)集合 ????????List<List<Map<String, String>>> ss =?new?ArrayList<List<Map<String, String>>>(); ????????for?(int?i =?0; i < names.length; i++) { ????????????// 提供父列表的數(shù)據(jù) ????????????Map<String, String> maps =?new?HashMap<String, String>(); ????????????maps.put("names", names[i]); ????????????group.add(maps); ????????????// 提供當(dāng)前父列的子列數(shù)據(jù) ????????????List<Map<String, String>> child =?new?ArrayList<Map<String, String>>(); ????????????for?(int?j =?0; j < names.length; j++) { ????????????????Map<String, String> mapsj =?new?HashMap<String, String>(); ????????????????mapsj.put("map", childnames[i][j]); ????????????????child.add(mapsj); ????????????} ????????????ss.add(child); ????????} ????????/** ?????????* 第一個(gè)參數(shù) 應(yīng)用程序接口 this 第二個(gè)父列List<?extends Map<String,Object>>集合 為父列提供數(shù)據(jù) ?????????* 第三個(gè)參數(shù) 父列顯示的組件資源文件 第四個(gè)參數(shù) 鍵值列表 父列Map字典的key 第五個(gè)要顯示的父列組件id 第六個(gè) 子列的顯示資源文件 ?????????* 第七個(gè)參數(shù) 鍵值列表的子列Map字典的key 第八個(gè)要顯示子列的組件id ?????????*/ ????????SimpleExpandableListAdapter expand =?new?SimpleExpandableListAdapter( ????????????????this, group, R.layout.images,?new?String[] {?"names"?}, ????????????????new?int[] { R.id.txtName }, ss, R.layout.items, ????????????????new?String[] {?"map"?},?new?int[] { R.id.items }); ????????ep = (ExpandableListView) findViewById(R.id.expanable_mylist); ????????ep.setAdapter(expand); ????} }

  效果跟上面相同:

?本文轉(zhuǎn)自Fly_Elephant博客園博客,原文鏈接:http://www.cnblogs.com/xiaofeixiang/p/4107356.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者

總結(jié)

以上是生活随笔為你收集整理的Android数据适配-ExpandableListView的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。