Android中ExpandableListView的用法實(shí)例
本文實(shí)例講述了Android中ExpandableListView的用法,ExpandableListView是android中可以實(shí)現(xiàn)下拉list的一個(gè)控件,具體的實(shí)現(xiàn)方法如下:
首先:在layout的xml文件中定義一個(gè)ExpandableListView
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
>
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
定義兩個(gè)List,用來(lái)存放控件中Group/Child中的String
private List<List<String>> childArray;
對(duì)這兩個(gè)List進(jìn)行初始化,并插入一些數(shù)據(jù)
childArray = new ArrayList<List<String>>();
groupArray.add("第一行");
groupArray.add("第二行");
List<String> tempArray = new ArrayList<String>();
tempArray.add("第一條");
tempArray.add("第二條");
tempArray.add("第三條");
for(int index = 0; index <groupArray.size(); ++index)
{
childArray.add(tempArray);
}
定義ExpandableListView的Adapter
public class ExpandableAdapter extends BaseExpandableListAdapter
{
Activity activity;
public ExpandableAdapter(Activity a)
{
activity = a;
}
public Object getChild(int groupPosition, int childPosition)
{
return childArray.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return childArray.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
String string = childArray.get(groupPosition).get(childPosition);
return getGenericView(string);
}
// group method stub
public Object getGroup(int groupPosition)
{
return groupArray.get(groupPosition);
}
public int getGroupCount()
{
return groupArray.size();
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String string = groupArray.get(groupPosition);
return getGenericView(string);
}
// View stub to create Group/Children 's View
public TextView getGenericView(String string)
{
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(activity);
text.setLayoutParams(layoutParams);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setText(string);
return text;
}
public boolean hasStableIds()
{
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
最后,給定義好的ExpandableListView添加上Adapter
expandableListView.setAdapter(new ExpandableAdapter(Main.this));
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- 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展開(kāi)列表控件使用實(shí)例
- Android ExpandableListView用法示例詳解
相關(guān)文章
Android實(shí)現(xiàn)圖片循環(huán)播放的實(shí)例方法
2013-05-05Android輔助功能實(shí)現(xiàn)自動(dòng)搶紅包(附源碼)
本篇文章主要介紹了Android輔助功能實(shí)現(xiàn)自動(dòng)搶紅包(附源碼),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Android中Bitmap常見(jiàn)的一些操作:縮放、裁剪、旋轉(zhuǎn)和偏移
Bitmap是Android中處理圖片的一個(gè)重要的類,下面這篇文章主要給大家介紹了關(guān)于Android中Bitmap常見(jiàn)的一些操作:縮放、裁剪、旋轉(zhuǎn)和偏移的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07Android 游戲開(kāi)發(fā)入門簡(jiǎn)單示例
本文主要介紹Android 游戲開(kāi)發(fā),這里整理了詳細(xì)的開(kāi)發(fā)游戲的資料,并提供簡(jiǎn)單的游戲示例代碼,有興趣的同學(xué)可以參考下2016-08-08Android開(kāi)發(fā)之獲取網(wǎng)絡(luò)鏈接狀態(tài)
這篇文章主要介紹了Android獲取網(wǎng)絡(luò)鏈接狀態(tài)的方法,主要是通過(guò)ConnectivityManager類來(lái)完成的,需要的朋友可以參考下2014-08-08詳解Android中Application設(shè)置全局變量以及傳值
這篇文章主要介紹了詳解Android中Application設(shè)置全局變量以及傳值的相關(guān)資料,希望通過(guò)本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09Android拖動(dòng)條的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android拖動(dòng)條的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09