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實(shí)現(xiàn)指紋識(shí)別功能
這篇文章主要介紹了android指紋識(shí)別功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09kotlin實(shí)現(xiàn)快遞與號(hào)碼歸屬地查詢(xún)案例詳解
時(shí)間軸時(shí)一個(gè)很炫酷的效果,一般作用在物流信息上,我們同樣也可以作為一個(gè)學(xué)習(xí)對(duì)象去學(xué)習(xí)他的使用方法,同時(shí)呢,我們可以在線查詢(xún)我們的電話號(hào)碼歸屬地,巧用鍵盤(pán)的邏輯提升我們用戶(hù)體驗(yàn)2023-02-02Android apk完整性檢測(cè)的實(shí)現(xiàn)思路和代碼實(shí)現(xiàn)
這篇文章主要介紹了Android apk完整性檢測(cè)的實(shí)現(xiàn)思路和代碼實(shí)現(xiàn),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12Android開(kāi)發(fā)實(shí)現(xiàn)仿QQ消息SwipeMenuListView滑動(dòng)刪除置頂功能【附源碼下載】
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)仿QQ消息SwipeMenuListView滑動(dòng)刪除置頂功能,結(jié)合實(shí)例形式分析了Android swipemenulistview相關(guān)組件的使用技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-12-12Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng)
這篇文章主要為大家詳細(xì)介紹了Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05輕松實(shí)現(xiàn)Rxjava定時(shí)器功能
這篇文章主要為大家詳細(xì)介紹了Rxjava實(shí)現(xiàn)定時(shí)器功能的兩種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android實(shí)現(xiàn)簡(jiǎn)單時(shí)鐘View的方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘View,關(guān)鍵點(diǎn)在Canvas的平移與旋轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08