Android中ExpandableListView的使用
2012-08-09 22:39
405 查看
ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下:
首先:在layout的xml文件中定义一个ExpandableListView
定义两个List,用来存放控件中Group/Child中的String
对这两个List进行初始化,并插入一些数据
定义ExpandableListView的Adapter
最后,给定义好的ExpandableListView添加上Adapter
首先:在layout的xml文件中定义一个ExpandableListView
< LinearLayout 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<String> groupArray; private List<List<String>> childArray;
对这两个List进行初始化,并插入一些数据
groupArray = new ArrayList<String>(); 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
//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 expandableListView = (ExpandableListView)findViewById(R.id.expandableListView); expandableListView.setAdapter(new ExpandableAdapter(Main.this ));
相关文章推荐
- Android ExpandableListView使用(1):使用二维数组封装数据
- Android中ExpandableListView的使用
- Android ExpandableListView的使用
- android 中可展开控件ExpandableListView的使用
- android 扩展列表ExpandableListView的详解和使用
- Android 仿QQ好友分组列表、ExpandableListView的使用详解
- Android 使用PullToRefreshExpandableListView不能setAdapter的问题
- 【Android】ExpandableListView的ContextMenu使用
- Android的ExpandableListView的动画展开效果和使用traceview的性能优化
- android之ExpandableListView的使用
- Android 之 ExpandableListView 的使用
- android ExpandableListView ExpandableListAdapter使用
- Android ExpandableListView使用小结(二)
- Android ExpandableListView展开列表控件使用实例
- Android中级联列表ExpandableListView使用
- android 高级之旅 (六)ExpandableListView 使用详解
- Android中ExpandableListView的使用
- Android ExpandableListView的使用
- Android 中使用ExpandableListView 实现分组的实例
- Android中ExpandableListView的使用