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

該控件需要一個主界面XML 一個標(biāo)題界面XML及一個列表內(nèi)容界面XML
首先我們來看看 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)題界面
我們只要放上一個要顯示出來的標(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>
接下來再上主代碼, 命名有點亂, 大家真正用于開發(fā)時可不要這樣命名啊.
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//創(chuàng)建二個一級條目標(biāo)題
Map<String, String> title_1 = new HashMap<String, String>();
Map<String, String> title_2 = new HashMap<String, String>();
title_1.put("group", "開發(fā)");
title_2.put("group", "管理");
//創(chuàng)建一級條目容器
List<Map<String, String>> gruops = new ArrayList<Map<String,String>>();
gruops.add(title_1);
gruops.add(title_2);
//創(chuàng)建二級條目內(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", "敏捷開發(fā)");
content_4.put("child", "迭代開發(fā)");
List<Map<String, String>> childs_2 = new ArrayList<Map<String,String>>();
childs_2.add(content_3);
childs_2.add(content_4);
//存放兩個內(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.一級集合 3.一級樣式文件 4. 一級條目鍵值 5.一級顯示控件名
// 6. 二級集合 7. 二級樣式 8.二級條目鍵值 9.二級顯示控件名
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);
}
//二級標(biāo)題按下
@Override
public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup)
{
// TODO Auto-generated method stub
return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
}
//一級標(biāo)題按下
@Override
public void setSelectedGroup(int groupPosition)
{
// TODO Auto-generated method stub
super.setSelectedGroup(groupPosition);
}
再最后, 運行你的模擬器就可以看見啦。
將上面的ExpandableListView控件化.
控件化比較簡單我們只要用普通的Activity類就可以了, 不用再繼承ExpandableListView.
只需要在成員變量中添加
private ExpandableListView expandList;
然后在添加內(nèi)容時改成
expandList.setAdapter(sela);
就可以了。 當(dāng)然, 響應(yīng)事件Listener也可以自己添加。
附:另一個例子
ExpandableListView的用法與ListView和GridView,Gallery 類似,都是通過一個Adapter來顯示.
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è)置那個小圖標(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;
}
}
注意下面還有一個ElvObject類
主程序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雙層嵌套實現(xiàn)三級樹形菜單
- Android ExpandableListView實現(xiàn)下拉刷新和加載更多效果
- Android ExpandableListView單選以及多選實現(xiàn)代碼
- Android ScrollView嵌套ExpandableListView顯示不正常的問題的解決辦法
- Android listview ExpandableListView實現(xiàn)多選,單選,全選,edittext實現(xiàn)批量輸入的實例代碼
- Android 關(guān)于ExpandableListView刷新問題的解決方法
- Android 關(guān)于ExpandableListView去掉里頭分割線的方法
- Android UI控件ExpandableListView基本用法詳解
- Android改變ExpandableListView的indicator圖標(biāo)實現(xiàn)方法
- Android中ExpandableListView的用法實例
- Android ExpandableListView用法示例詳解
相關(guān)文章
Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
最近在做一個 Android 項目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來與大家分享2013-01-01
Android利用CountDownTimer實現(xiàn)驗證碼倒計時效果實例
這篇文章主要給大家介紹了關(guān)于Android如何利用CountDownTimer實現(xiàn)驗證碼倒計時效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Android開發(fā)Jetpack組件ViewModel使用講解
這篇文章主要介紹了Android?Jetpack架構(gòu)組件?ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲和管理界面相關(guān)的數(shù)據(jù),感興趣可以來學(xué)習(xí)一下2022-08-08
Android LayoutTransiton實現(xiàn)簡單的錄制按鈕
這篇文章主要介紹了Android LayoutTransiton實現(xiàn)簡單的錄制按鈕,主要實現(xiàn)開始,暫停,停止和顯示錄制時間長度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Android端內(nèi)數(shù)據(jù)狀態(tài)同步方案VM-Mapping詳解
這篇文章主要介紹了Android端內(nèi)數(shù)據(jù)狀態(tài)同步方案VM-Mapping詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Android 中 MD5 的幾種生成方式(小結(jié))
這篇文章主要介紹了Android 中 MD5 的幾種生成方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

