Android可展开的列表组件(ExpandableListView)模板
2016-09-08 10:07
651 查看
主程序
布局文件
ExpandableListAdapter adapter =new BaseExpandableListAdapter() { int[] logos = new int[] { R.drawable.p, R.drawable.z, R.drawable.t }; String[] armTypes = new String[] { "神族兵种", "虫族兵种", "人族兵种"}; String[][] arms = new String[][] { { "狂战士", "龙骑士", "黑暗圣堂", "电兵" }, { "小狗", "刺蛇", "飞龙", "自爆飞机" }, { "机枪兵", "护士MM" , "幽灵" } }; @Override public Object getChild(int groupPosition, int childPosition) { return arms[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return arms[groupPosition].length; } private TextView getTextView() { TextView textView = new TextView(MainActivity.this); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);//设置textView中文字的对齐方式 textView.setTextSize(20); return textView; } // 该方法决定每个子选项的外观 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64);//设置TextView的宽和高 textView.setLayoutParams(lp); textView.setPadding(120, 0, 0, 0);//设置TextView左右上下的边距 textView.setTextSize(12); textView.setText(getChild(groupPosition, childPosition) .toString()); return textView; } // 获取指定组位置处的组数据 @Override public Object getGroup(int groupPosition) { return armTypes[groupPosition]; } @Override public int getGroupCount() { return armTypes.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } // 该方法决定每个组选项的外观 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); ImageView logo = new ImageView(MainActivity.this); logo.setImageResource(logos[groupPosition]); ll.addView(logo); TextView textView = getTextView(); AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 200); textView.setLayoutParams(lp); textView.setPadding(12, 0, 0, 0); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } }; ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list); expandListView.setAdapter(adapter);
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ExpandableListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:childIndicator="@drawable/ic_launcher"/> </LinearLayout>
相关文章推荐
- Android 可展开列表组件 ExpandableListView
- Android中可展开的列表组件(ExpandableListView)的使用
- Android 可展开列表组件 ExpandableListView
- Android ExpandableListView展开列表控件使用实例
- Android 禁止ExpandableListView组的点击展开或隐藏事件,让所有列表都展开
- ExpandableListView(可展开的列表组件)使用方法
- Android学习第五天————ExpandableListView组件通过适配器BaseExpandableListAdapter实现两层列表项
- ExpandableListview控件使用---可展开的列表组件
- ExpandableListView-可展开的列表组件
- Android ExpandableListView每组选一个值,并展开列表
- 可展开的列表组件 ExpandableListview
- 可展开的列表组件——ExpandableListView深入解析
- Android ExpandableListView 展开列表控件(手机QQ好友列表) (二)控件版
- 一看就会Android之分组列表组件ExpandableListView的使用
- Android ExpandableListView 展开列表控件
- Android开发高级组件--ExpandableListView(可伸展的列表组件)
- 可展开列表组件ExpandableListView解析
- 可展开的列表组件ExpandableListView
- ExpandableListView--可展开的列表组件
- ExpandableListView(可展开的列表组件)的说明以及其用法