Android中ExpandableListView的用法實例
本文實例講述了Android中ExpandableListView的用法,ExpandableListView是android中可以實現(xiàn)下拉list的一個控件,具體的實現(xiàn)方法如下:
首先:在layout的xml文件中定義一個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>
定義兩個List,用來存放控件中Group/Child中的String
private List<List<String>> childArray;
對這兩個List進行初始化,并插入一些數(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));
希望本文所述對大家的Android程序設(shè)計有所幫助。
- 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圖標實現(xiàn)方法
- Android ExpandableListView展開列表控件使用實例
- Android ExpandableListView用法示例詳解
相關(guān)文章
Android實現(xiàn)圖片循環(huán)播放的實例方法
2013-05-05Android中Bitmap常見的一些操作:縮放、裁剪、旋轉(zhuǎn)和偏移
Bitmap是Android中處理圖片的一個重要的類,下面這篇文章主要給大家介紹了關(guān)于Android中Bitmap常見的一些操作:縮放、裁剪、旋轉(zhuǎn)和偏移的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-07-07Android開發(fā)之獲取網(wǎng)絡(luò)鏈接狀態(tài)
這篇文章主要介紹了Android獲取網(wǎng)絡(luò)鏈接狀態(tài)的方法,主要是通過ConnectivityManager類來完成的,需要的朋友可以參考下2014-08-08詳解Android中Application設(shè)置全局變量以及傳值
這篇文章主要介紹了詳解Android中Application設(shè)置全局變量以及傳值的相關(guān)資料,希望通過本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09