Android ExpandableListView展開(kāi)列表控件使用實(shí)例
你是否覺(jué)得手機(jī)QQ上的好友列表那個(gè)控件非常棒? 不是..... 那也沒(méi)關(guān)系,學(xué)多一點(diǎn)知識(shí)對(duì)自己也有益無(wú)害。
那么我們就開(kāi)始吧。
展開(kāi)型列表控件, 原名ExpandableListView
是普通的列表控件進(jìn)階版, 可以自由的把列表進(jìn)行收縮, 非常的方便兼好看。
首先看看我完成的截圖, 雖然界面不漂亮, 但大家可以自己去修改界面。

該控件需要一個(gè)主界面XML 一個(gè)標(biāo)題界面XML及一個(gè)列表內(nèi)容界面XML
首先我們來(lái)看看 mian.xml 主界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
groups.xml 該界面是父標(biāo)題界面
我們只要放上一個(gè)要顯示出來(lái)的標(biāo)題TextView控件上去即可
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textGroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="40px"
android:paddingTop="6px"
android:paddingBottom="6px"
android:textSize="15sp"
android:text="No data"
/>
</LinearLayout>
childs.xml 是子控件, 直接顯示列表內(nèi)容
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textChild"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="60px"
android:paddingTop="10px"
android:paddingBottom="10px"
android:textSize="20sp"
android:text="No Data"
/>
</LinearLayout>
接下來(lái)再上主代碼, 命名有點(diǎn)亂, 大家真正用于開(kāi)發(fā)時(shí)可不要這樣命名啊.
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//創(chuàng)建二個(gè)一級(jí)條目標(biāo)題
Map<String, String> title_1 = new HashMap<String, String>();
Map<String, String> title_2 = new HashMap<String, String>();
title_1.put("group", "開(kāi)發(fā)");
title_2.put("group", "管理");
//創(chuàng)建一級(jí)條目容器
List<Map<String, String>> gruops = new ArrayList<Map<String,String>>();
gruops.add(title_1);
gruops.add(title_2);
//創(chuàng)建二級(jí)條目?jī)?nèi)容
//內(nèi)容一
Map<String, String> content_1 = new HashMap<String, String>();
Map<String, String> content_2 = new HashMap<String, String>();
content_1.put("child", "VC++");
content_2.put("child", "Java");
List<Map<String, String>> childs_1 = new ArrayList<Map<String,String>>();
childs_1.add(content_1);
childs_1.add(content_2);
//內(nèi)容二
Map<String, String> content_3 = new HashMap<String, String>();
Map<String, String> content_4 = new HashMap<String, String>();
content_3.put("child", "敏捷開(kāi)發(fā)");
content_4.put("child", "迭代開(kāi)發(fā)");
List<Map<String, String>> childs_2 = new ArrayList<Map<String,String>>();
childs_2.add(content_3);
childs_2.add(content_4);
//存放兩個(gè)內(nèi)容, 以便顯示在列表中
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(childs_1);
childs.add(childs_2);
//創(chuàng)建ExpandableList的Adapter容器
//參數(shù): 1.上下文 2.一級(jí)集合 3.一級(jí)樣式文件 4. 一級(jí)條目鍵值 5.一級(jí)顯示控件名
// 6. 二級(jí)集合 7. 二級(jí)樣式 8.二級(jí)條目鍵值 9.二級(jí)顯示控件名
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
this, gruops, R.drawable.groups, new String[]{"group"}, new int[]{R.id.textGroup},
childs, R.drawable.childs, new String[]{"child"}, new int[]{R.id.textChild}
);
//加入列表
setListAdapter(sela);
}
}
//最后, 如果想響應(yīng)各操作的話, 就要重載下面的方法
//列表內(nèi)容按下
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
// TODO Auto-generated method stub
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
//二級(jí)標(biāo)題按下
@Override
public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup)
{
// TODO Auto-generated method stub
return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
}
//一級(jí)標(biāo)題按下
@Override
public void setSelectedGroup(int groupPosition)
{
// TODO Auto-generated method stub
super.setSelectedGroup(groupPosition);
}
再最后, 運(yùn)行你的模擬器就可以看見(jiàn)啦。
將上面的ExpandableListView控件化.
控件化比較簡(jiǎn)單我們只要用普通的Activity類(lèi)就可以了, 不用再繼承ExpandableListView.
只需要在成員變量中添加
private ExpandableListView expandList;
然后在添加內(nèi)容時(shí)改成
expandList.setAdapter(sela);
就可以了。 當(dāng)然, 響應(yīng)事件Listener也可以自己添加。
附:另一個(gè)例子
ExpandableListView的用法與ListView和GridView,Gallery 類(lèi)似,都是通過(guò)一個(gè)Adapter來(lái)顯示.
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="@+id/elv" android:indicatorRight="160dp"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</ExpandableListView>
<!-- indicatorRight 設(shè)置那個(gè)小圖標(biāo)右邊緣與 ExpandableListView左邊緣的間距-->
</LinearLayout>
ElvAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class ElvAdapter extends BaseExpandableListAdapter
{
ArrayList<ElvObject> objs;
Context context;
ElvAdapter(Context context,ArrayList<ElvObject> objs)
{
this.objs=objs;
this.context=context;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return objs.get(groupPosition).childs.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
//子元素的View
TextView tv=new TextView(context);
tv.setText(objs.get(groupPosition).childs.get(childPosition));
tv.setLayoutParams(new ExpandableListView.LayoutParams(ExpandableListView.LayoutParams.FILL_PARENT,ExpandableListView.LayoutParams.WRAP_CONTENT));
return tv;
}
@Override
public int getChildrenCount(int groupPosition)
{
// TODO Auto-generated method stub
return objs.get(groupPosition).childs.size();
}
@Override
public Object getGroup(int groupPosition)
{
// TODO Auto-generated method stub
return objs.get(groupPosition);
}
@Override
public int getGroupCount()
{
// TODO Auto-generated method stub
return objs.size();
}
@Override
public long getGroupId(int groupPosition)
{
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
//分組的View
TextView tv=new TextView(context);
tv.setText(objs.get(groupPosition).groupName);
ExpandableListView.LayoutParams params=new ExpandableListView.LayoutParams(ExpandableListView.LayoutParams.FILL_PARENT,60);
tv.setLayoutParams(params);
return tv;
}
@Override
public boolean hasStableIds()
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return true;
}
}
class ElvObject{
String groupName="";
ArrayList<String> childs=new ArrayList<String>();
ElvObject(String groupName,ArrayList<String> childs)
{
this.groupName=groupName;
this.childs=childs;
}
}
注意下面還有一個(gè)ElvObject類(lèi)
主程序AndroidTestActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class AndroidTestActivity extends Activity
{
/** Called when the activity is first created. */
ExpandableListView elv;
ElvAdapter adapter;
ArrayList<ElvObject> objs=new ArrayList<ElvObject>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
elv=(ExpandableListView)findViewById(R.id.elv);
adapter=new ElvAdapter(this,objs);
elv.setAdapter(adapter);
ArrayList<String> list=new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
objs.add(new ElvObject("英文",list));
ArrayList<String> list2=new ArrayList<String>();
list2.add("111");
list2.add("222");
list2.add("333");
list2.add("444");
objs.add(new ElvObject("數(shù)字",list2));
}
}
- Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級(jí)樹(shù)形菜單
- Android ExpandableListView實(shí)現(xiàn)下拉刷新和加載更多效果
- Android ExpandableListView單選以及多選實(shí)現(xiàn)代碼
- Android ScrollView嵌套ExpandableListView顯示不正常的問(wèn)題的解決辦法
- Android listview ExpandableListView實(shí)現(xiàn)多選,單選,全選,edittext實(shí)現(xiàn)批量輸入的實(shí)例代碼
- Android 關(guān)于ExpandableListView刷新問(wèn)題的解決方法
- Android 關(guān)于ExpandableListView去掉里頭分割線的方法
- Android UI控件ExpandableListView基本用法詳解
- Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
- Android中ExpandableListView的用法實(shí)例
- Android ExpandableListView用法示例詳解
相關(guān)文章
Android打開(kāi)GPS導(dǎo)航并獲取位置信息返回null解決方案
最近在做一個(gè) Android 項(xiàng)目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡(jiǎn)單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來(lái)與大家分享2013-01-01
Android利用CountDownTimer實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)效果實(shí)例
這篇文章主要給大家介紹了關(guān)于Android如何利用CountDownTimer實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Android開(kāi)發(fā)Jetpack組件ViewModel使用講解
這篇文章主要介紹了Android?Jetpack架構(gòu)組件?ViewModel詳解,ViewModel類(lèi)讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類(lèi)旨在以注重生命周期的方式存儲(chǔ)和管理界面相關(guān)的數(shù)據(jù),感興趣可以來(lái)學(xué)習(xí)一下2022-08-08
Android LayoutTransiton實(shí)現(xiàn)簡(jiǎn)單的錄制按鈕
這篇文章主要介紹了Android LayoutTransiton實(shí)現(xiàn)簡(jiǎn)單的錄制按鈕,主要實(shí)現(xiàn)開(kāi)始,暫停,停止和顯示錄制時(shí)間長(zhǎng)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android通過(guò)BLE傳輸文件遇到問(wèn)題解決
這篇文章主要為大家介紹了Android通過(guò)BLE傳輸文件遇到問(wèn)題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android 集成Google Cast 異常問(wèn)題解析
這篇文章主要為大家介紹了Android 集成Google Cast 異常問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
優(yōu)化和瘦身Android APK的六個(gè)小技巧
Android應(yīng)用的大小對(duì)用戶體驗(yàn)和應(yīng)用性能至關(guān)重要,大型APK文件會(huì)增加應(yīng)用的安裝時(shí)間,啟動(dòng)時(shí)間和頁(yè)面加載時(shí)間,降低了用戶體驗(yàn),因此,APK瘦身是Android開(kāi)發(fā)中的重要任務(wù),在本文中,我們將分享6個(gè)小技巧,幫助你優(yōu)化和瘦身Android應(yīng)用,需要的朋友可以參考下2023-11-11
Android EditText每4位自動(dòng)添加空格效果
這篇文章主要給大家介紹了關(guān)于Android EditText每4位自動(dòng)添加空格效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用EditText具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Android端內(nèi)數(shù)據(jù)狀態(tài)同步方案VM-Mapping詳解
這篇文章主要介紹了Android端內(nèi)數(shù)據(jù)狀態(tài)同步方案VM-Mapping詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Android 中 MD5 的幾種生成方式(小結(jié))
這篇文章主要介紹了Android 中 MD5 的幾種生成方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

