Android UI控件ExpandableListView基本用法詳解
ExpandableListView介紹
ExpandableListView的引入
ExpandableListView可以顯示一個(gè)視圖垂直滾動(dòng)顯示兩級列表中的條目,這不同于列表視圖(ListView)。ExpandableListView允許有兩個(gè)層次:一級列表中有二級列表。
比如在手機(jī)設(shè)置中,對于分類,有很好的效果。手機(jī)版QQ也是這樣的效果。
使用ExpandableListView的整體思路
(1)給ExpandableListView設(shè)置適配器,那么必須先設(shè)置數(shù)據(jù)源。
(2)數(shù)據(jù)源,就是此處的適配器類ExpandableAdapter,此方法繼承了BaseExpandableListAdapter,需要重寫里面的10個(gè)方法。
數(shù)據(jù)源中,用到了自定義的View布局,此時(shí)根據(jù)自己的需求,來設(shè)置組和子項(xiàng)的布局樣式。
getChildView()和getGroupView()方法設(shè)置自定義布局。
(3)數(shù)據(jù)源設(shè)置好,直接給ExpandableListView.setAdapter()即可實(shí)現(xiàn)此收縮功能。
ExpandableListView的完整代碼實(shí)現(xiàn)
(1)activity_main.xml:在里面放置一個(gè)ExpandableListView控件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.smyhvae.expandablelistviewdemo.MainActivity"> <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
(2)item_group.xml:一級列表的item的布局
<?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="wrap_content" android:background="#cccccc" android:orientation="horizontal"> <TextView android:id="@+id/tv_group" android:layout_width="wrap_content" android:layout_height="30dp" android:gravity="center" android:text="group text" android:textColor="#000000" /> </LinearLayout>
(3)item_child.xml:二級列表的item的布局
<?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="wrap_content" android:gravity="center" android:orientation="horizontal"> <ImageView android:id="@+id/iv_child" android:layout_width="30dp" android:layout_height="30dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/tv_child" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="item text" android:textColor="#000000"/> </LinearLayout>
(4)MainActivity.java:
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { //View private ExpandableListView expandableListView; //Model:定義的數(shù)據(jù) private String[] groups = {"A", "B", "C"}; //注意,字符數(shù)組不要寫成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}} private String[][] childs = {{"A1", "A2", "A3", "A4"}, {"A1", "A2", "A3", "B4"}, {"A1", "A2", "A3", "C4"}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expandableListView = (ExpandableListView) findViewById(R.id.expandableListView); expandableListView.setAdapter(new MyExpandableListView()); } //為ExpandableListView自定義適配器 class MyExpandableListView extends BaseExpandableListAdapter { //返回一級列表的個(gè)數(shù) @Override public int getGroupCount() { return groups.length; } //返回每個(gè)二級列表的個(gè)數(shù) @Override public int getChildrenCount(int groupPosition) { //參數(shù)groupPosition表示第幾個(gè)一級列表 Log.d("smyhvae", "-->" + groupPosition); return childs[groupPosition].length; } //返回一級列表的單個(gè)item(返回的是對象) @Override public Object getGroup(int groupPosition) { return groups[groupPosition]; } //返回二級列表中的單個(gè)item(返回的是對象) @Override public Object getChild(int groupPosition, int childPosition) { return childs[groupPosition][childPosition]; //不要誤寫成groups[groupPosition][childPosition] } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } //每個(gè)item的id是否是固定?一般為true @Override public boolean hasStableIds() { return true; } //【重要】填充一級列表 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.item_group, null); } else { } TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group); tv_group.setText(groups[groupPosition]); return convertView; } //【重要】填充二級列表 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.item_child, null); } ImageView iv_child = (ImageView) convertView.findViewById(R.id.iv_child); TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child); //iv_child.setImageResource(resId); tv_child.setText(childs[groupPosition][childPosition]); return convertView; } //二級列表中的item是否能夠被選中?可以改為true @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }
注:請自行完成ConvertView和ViewHolder的優(yōu)化。
工程文件:(Android Studio 2.1)http://xiazai.jb51.net/201609/yuanma/AndroidExpandableListView(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級樹形菜單
- Android ExpandableListView實(shí)現(xiàn)下拉刷新和加載更多效果
- Android ExpandableListView單選以及多選實(shí)現(xiàn)代碼
- Android ScrollView嵌套ExpandableListView顯示不正常的問題的解決辦法
- Android listview ExpandableListView實(shí)現(xiàn)多選,單選,全選,edittext實(shí)現(xiàn)批量輸入的實(shí)例代碼
- Android 關(guān)于ExpandableListView刷新問題的解決方法
- Android 關(guān)于ExpandableListView去掉里頭分割線的方法
- Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
- Android中ExpandableListView的用法實(shí)例
- Android ExpandableListView展開列表控件使用實(shí)例
- Android ExpandableListView用法示例詳解
相關(guān)文章
android自動(dòng)工具類TextUtils使用詳解
這篇文章主要介紹了android自動(dòng)工具類TextUtils的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android仿視頻加載旋轉(zhuǎn)小球動(dòng)畫效果的實(shí)例代碼
這篇文章主要介紹了Android仿視頻加載旋轉(zhuǎn)小球動(dòng)畫效果的實(shí)例代碼,文中給大家提到了PathMeasure的用法,介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09Android性能優(yōu)化之弱網(wǎng)優(yōu)化詳解
這篇文章主要為大家介紹了Android性能優(yōu)化之弱網(wǎng)優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android App啟動(dòng)圖啟動(dòng)界面(Splash)的簡單實(shí)現(xiàn)代碼
這篇文章主要介紹了Android App啟動(dòng)圖啟動(dòng)界面(Splash)的簡單實(shí)現(xiàn)代碼,本文通過實(shí)例圖文詳解相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Android仿英語流利說取詞放大控件的實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了Android仿英語流利說取詞放大控件的實(shí)現(xiàn)方法,較為詳細(xì)的分析了取詞放大控件的實(shí)現(xiàn)步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02Android ViewPager實(shí)現(xiàn)無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)無限循環(huán)效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03點(diǎn)擊圖標(biāo)進(jìn)入指定瀏覽器將首頁設(shè)置全透明解決一閃而過問題
進(jìn)入瀏覽器之前有一個(gè)頁面閃了一下,那是因?yàn)閺哪莻€(gè)空白的首頁跳過去的。解決的辦法是把他變成透明的就好了2014-08-08Android自定義View實(shí)現(xiàn)進(jìn)度條動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)進(jìn)度條動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08android 獲取文件的擴(kuò)展名和去掉文件擴(kuò)展名的小例子
android 獲取文件的擴(kuò)展名和去掉文件擴(kuò)展名的小例子,需要的朋友可以參考一下2013-06-06