Android數據適配-ExpandableListView

Android中ListView的用法基本上學的時候都會使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去實現,這次主要使用的ExpandableListView展示一種兩層的效果,ExpandableListView是android中可以實現下拉list的一個控件類似于QQ那種我好友之后就是一排自己的好友,就是兩層效果,實現的話使用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"?/>

?定義一個省份的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>

定義了一個地區的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實現

主要實現代碼,代碼中都已經注釋,其中最主要的SimpleExpandableListAdapter中的參數,這個參數太多,很容易弄錯,可以看下注釋或者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
// 創建一級條目
?List<Map<String, String>> provinces =?new?ArrayList<Map<String, String>>();
?//創建兩個省份一級條目
?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);
?// 創建一級條目下的的二級地區條目
?List<Map<String, String>> childList1=?new?ArrayList<Map<String, String>>();
?//同樣是在一級條目目錄下創建兩個對應的二級條目目錄
?Map<String, String> child1=?new?HashMap<String, String>();
?child1.put("child",?"鄭州");
?Map<String, String> child2 =?new?HashMap<String, String>();
?child2.put("child",?"開封");
?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);
?// 將二級條目放在一個集合里,供顯示時使用
?List<List<Map<String, String>>> childs =?new?ArrayList<List<Map<String, String>>>();
?childs.add(childList1);
?childs.add(childList2);
?/**
??* 使用SimpleExpandableListAdapter顯示ExpandableListView
??* 參數1.上下文對象Context
??* 參數2.一級條目目錄集合
??* 參數3.一級條目對應的布局文件
??* 參數4.fromto,就是map中的key,指定要顯示的對象
??* 參數5.與參數4對應,指定要顯示在groups中的id
??* 參數6.二級條目目錄集合
??* 參數7.二級條目對應的布局文件
??* 參數8.fromto,就是map中的key,指定要顯示的對象
??* 參數9.與參數8對應,指定要顯示在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);

這個mainActivity需要繼承ExpandableListActivity,當然你可以設置其中的點擊事件,只要重寫一下方法即可:

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
/**
?* 設置哪個二級目錄被默認選中
?*/
@Override
public?boolean?setSelectedChild(int?groupPosition,?int?childPosition,
????????boolean?shouldExpandGroup) {
????????//do something
????return?super.setSelectedChild(groupPosition, childPosition,
????????????shouldExpandGroup);
}
/**
?* 設置哪個一級目錄被默認選中
?*/
@Override
public?void?setSelectedGroup(int?groupPosition) {
????//do something
????super.setSelectedGroup(groupPosition);
}
/**
?* 當二級條目被點擊時響應
?*/
@Override
public?boolean?onChildClick(ExpandableListView parent, View v,
????????int?groupPosition,?int?childPosition,?long?id) {
????????//do something
????return?super.onChildClick(parent, v, groupPosition, childPosition, id);
}

?效果如下:

?

上面這個例子寫的有點單調,其實第二個你子的布局直接是空的也行,例如定義一個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>

然后定義一個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>

 代碼調用:

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 {
????/**
?????* 實現可擴展展開列ExpandableListView的三種方式
?????* 一是使用SimpleExpandableListAdpater將兩個List集合包裝成ExpandableListView 二是
?????* 擴展BaseExpandableListAdpter
?????* 三是使用simpleCursorTreeAdapter將Cursor中的數據包裝成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);
????????// 定義父列表項List數據集合
????????List<Map<String, String>> group =?new?ArrayList<Map<String, String>>();
????????// 定義子列表項List數據集合
????????List<List<Map<String, String>>> ss =?new?ArrayList<List<Map<String, String>>>();
????????for?(int?i =?0; i < names.length; i++) {
????????????// 提供父列表的數據
????????????Map<String, String> maps =?new?HashMap<String, String>();
????????????maps.put("names", names[i]);
????????????group.add(maps);
????????????// 提供當前父列的子列數據
????????????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);
????????}
????????/**
?????????* 第一個參數 應用程序接口 this 第二個父列List<?extends Map<String,Object>>集合 為父列提供數據
?????????* 第三個參數 父列顯示的組件資源文件 第四個參數 鍵值列表 父列Map字典的key 第五個要顯示的父列組件id 第六個 子列的顯示資源文件
?????????* 第七個參數 鍵值列表的子列Map字典的key 第八個要顯示子列的組件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);
????}
}

  效果跟上面相同:

?本文轉自Fly_Elephant博客園博客,原文鏈接:http://www.cnblogs.com/xiaofeixiang/p/4107356.html,如需轉載請自行聯系原作者

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392303.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392303.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392303.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

JavaWeb 命名規則

命名規范命名規范命名規范命名規范 本規范主要針對java開發制定的規范項目命名項目命名項目命名項目命名 項目創建&#xff0c;名稱所有字母均小寫&#xff0c;組合方式為&#xff1a;com.company.projectName.component.hiberarchy。1. projectName&#xff1a;項目名稱2. com…

多元概率密度_利用多元論把握事件概率

多元概率密度Humans have plenty of cognitive strengths, but one area that most of us struggle with is estimating, explaining and preparing for improbable events. This theme underpins two of Nassim Taleb’s major works: Fooled by Randomness and The Black Swa…

nginx php訪問日志配置,nginx php-fpm 輸出php錯誤日志的配置方法

由于nginx僅是一個web服務器&#xff0c;因此nginx的access日志只有對訪問頁面的記錄&#xff0c;不會有php 的 error log信息。nginx把對php的請求發給php-fpm fastcgi進程來處理&#xff0c;默認的php-fpm只會輸出php-fpm的錯誤信息&#xff0c;在php-fpm的errors log里也看不…

阿里的技術愿景_技術技能的另一面:領域知識和長期愿景

阿里的技術愿景by Sihui Huang黃思慧 技術技能的另一面&#xff1a;領域知識和長期愿景 (The other side of technical skill: domain knowledge and long-term vision) When we first start our careers as software engineers, we tend to focus on improving our coding sk…

leetcode 721. 賬戶合并(并查集)

給定一個列表 accounts&#xff0c;每個元素 accounts[i] 是一個字符串列表&#xff0c;其中第一個元素 accounts[i][0] 是 名稱 (name)&#xff0c;其余元素是 emails 表示該賬戶的郵箱地址。 現在&#xff0c;我們想合并這些賬戶。如果兩個賬戶都有一些共同的郵箱地址&#…

es6重點筆記:數值,函數和數組

本篇全是重點&#xff0c;撿常用的懟&#xff0c;數值的擴展比較少&#xff0c;所以和函數放一起&#xff1a; 一&#xff0c;數值 1&#xff0c;Number.EPSILON&#xff1a;用來檢測浮點數的計算&#xff0c;如果誤差小于這個&#xff0c;就無誤 2&#xff0c;Math.trunc()&am…

SMSSMS垃圾郵件檢測器的專業攻擊

Note: The methodology behind the approach discussed in this post stems from a collaborative publication between myself and Irene Anthi.注意&#xff1a; 本文討論的方法背后的方法來自 我本人和 Irene Anthi 之間 的 合作出版物 。 介紹 (INTRODUCTION) Spam SMS te…

php pdo 緩沖,PDO支持數據緩存_PHP教程

/*** 作者&#xff1a;初十* QQ&#xff1a;345610000*/class myPDO extends PDO{public $cache_Dir null; //緩存目錄public $cache_expireTime 7200; //緩存時間&#xff0c;默認兩小時//帶緩存的查詢public function cquery($sql){//緩存存放總目錄if ($this->cache_Di…

mooc課程下載_如何使用十大商學院的免費課程制作MOOC“ MBA”

mooc課程下載by Laurie Pickard通過勞里皮卡德(Laurie Pickard) 如何使用十大商學院的免費課程制作MOOC“ MBA” (How to make a MOOC “MBA” using free courses from Top 10 business schools) Back when massive open online courses (MOOCs) were new, I started a proje…

leetcode 1584. 連接所有點的最小費用(并查集)

給你一個points 數組&#xff0c;表示 2D 平面上的一些點&#xff0c;其中 points[i] [xi, yi] 。 連接點 [xi, yi] 和點 [xj, yj] 的費用為它們之間的 曼哈頓距離 &#xff1a;|xi - xj| |yi - yj| &#xff0c;其中 |val| 表示 val 的絕對值。 請你返回將所有點連接的最小…

Nagios學習實踐系列

其實上篇Nagios學習實踐系列——基本安裝篇只是安裝了Nagios基本組件&#xff0c;雖然能夠打開主頁&#xff0c;但是如果不配置相關配置文件文件&#xff0c;那么左邊菜單很多頁面都打不開&#xff0c;相當于只是一個空殼子。接下來&#xff0c;我們來學習研究一下Nagios的配置…

在Salesforce中處理Email的發送

在Salesforce中可以用自帶的 Messaging 的 sendEmail 方法去處理Email的發送 請看如下一段簡單代碼&#xff1a; public boolean TextFormat {get;set;} public string EmailTo {get;set;} public string EmailCC {get;set;} public string EmailBCC {get;set;} public string …

kvm vnc的使用,鼠標漂移等

1.宿主機的vnc&#xff08;virtual Network Computing&#xff09;配置 安裝rpm包 yum install tigervnc-server -y 為了防止干擾直接關閉防火墻和selinux /etc/init.d/iptables stop setenforce 0 配置vnc密碼和啟動vncserver服務 vncpasswd vncserver 2.客戶機的vnc 在qemu…

php深淺拷貝,JavaScript 中的深淺拷貝

工作中經常會遇到需要復制 JavaScript 數據的時候&#xff0c;遇到 bug 時實在令人頭疼&#xff1b;面試中也經常會被問到如何實現一個數據的深淺拷貝&#xff0c;但是你對其中的原理清晰嗎&#xff1f;一起來看一下吧&#xff01;一、為什么會有深淺拷貝想要更加透徹的理解為什…

使用Python進行地理編碼和反向地理編碼

Geocoding is the process of taking input text, such as an address or the name of a place, and returning a latitude/longitude location. To put it simply, Geocoding is converting physical address to latitude and longitude.地理編碼是獲取輸入文本(例如地址或地點…

java開發簡歷編寫_如何通過幾個簡單的步驟編寫出色的初級開發人員簡歷

java開發簡歷編寫So you’ve seen your dream junior developer role advertised, and are thinking about applying. It’s time to write that Resume! Nothing better than sitting down to a blank piece of paper and not knowing how to start, right?因此&#xff0c;您…

leetcode 628. 三個數的最大乘積(排序)

給定一個整型數組&#xff0c;在數組中找出由三個數組成的最大乘積&#xff0c;并輸出這個乘積。 示例 1: 輸入: [1,2,3] 輸出: 6 解題思路 最大的乘積可能有兩種情況 1.兩個最小負數和一個最大正數 2.三個最大正數 代碼 class Solution {public int maximumProduct(int[…

[Object-C語言隨筆之三] 類的創建和實例化以及函數的添加和調用!

上一小節的隨筆寫了常用的打印以及很基礎的數據類型的定義方式&#xff0c;今天就來一起學習下如何創建類與函數的一些隨筆&#xff1b; 首先類的創建&#xff1a;在Xcode下&#xff0c;菜單File&#xff0d;New File&#xff0c;然后出現選擇class模板&#xff0c;如下圖&…

2024-AI人工智能學習-安裝了pip install pydot但是還是報錯

2024-AI人工智能學習-安裝了pip install pydot但是還是報錯 出現這樣子的錯誤&#xff1a; /usr/local/bin/python3.11 /Users/wangyang/PycharmProjects/studyPython/tf_model.py 2023-12-24 22:59:02.238366: I tensorflow/core/platform/cpu_feature_guard.cc:182] This …

grafana 創建儀表盤_創建儀表盤前要問的三個問題

grafana 創建儀表盤可視化 (VISUALIZATIONS) It’s easier than ever to dive into dashboarding, but are you doing it right?深入儀表板比以往任何時候都容易&#xff0c;但是您這樣做正確嗎&#xff1f; Tableau, Power BI, and many other business intelligence tools …