Android ExpandableListView 展开列表控件
2014-11-24 17:48
603 查看
虽然界面不漂亮, 但大家可以自己去修改界面。
该控件需要一个主界面XML 一个标题界面XML及一个列表内容界面XML
首先我们来看看 mian.xml 主界面
//该界面非常简单, 只要一个ExpandableListView即可
groups.xml 该界面是父标题界面
我们只要放上一个要显示出来的标题TextView控件上去即可
childs.xml 是子控件, 直接显示列表内容
接下来再上主代码, 命名有点乱, 大家真正用于开发时可不要这样命名啊.
最后, 如果想响应各操作的话, 就要重载下面的方法
运行即可。
该控件需要一个主界面XML 一个标题界面XML及一个列表内容界面XML
首先我们来看看 mian.xml 主界面
//该界面非常简单, 只要一个ExpandableListView即可
<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 该界面是父标题界面
我们只要放上一个要显示出来的标题TextView控件上去即可
<LinearLayout 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 是子控件, 直接显示列表内容
<LinearLayout 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>
接下来再上主代码, 命名有点乱, 大家真正用于开发时可不要这样命名啊.
public class ExpandActivity extends ExpandableListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建二个一级条目标题 Map<String, String> title_1 = new HashMap<String, String>(); Map<String, String> title_2 = new HashMap<String, String>(); title_1.put("group", "开发"); title_2.put("group", "管理"); //创建一级条目容器 List<Map<String, String>> gruops = new ArrayList<Map<String,String>>(); gruops.add(title_1); gruops.add(title_2); //创建二级条目内容 //内容一 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); //内容二 Map<String, String> content_3 = new HashMap<String, String>(); Map<String, String> content_4 = new HashMap<String, String>(); content_3.put("child", "敏捷开发"); content_4.put("child", "迭代开发"); List<Map<String, String>> childs_2 = new ArrayList<Map<String,String>>(); childs_2.add(content_3); childs_2.add(content_4); //存放两个内容, 以便显示在列表中 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>(); childs.add(childs_1); childs.add(childs_2); //创建ExpandableList的Adapter容器 //参数: 1.上下文 2.一级集合 3.一级样式文件 4. 一级条目键值 5.一级显示控件名 // 6. 二级集合 7. 二级样式 8.二级条目键值 9.二级显示控件名 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); } }
最后, 如果想响应各操作的话, 就要重载下面的方法
//列表内容按下 @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); } //二级标题按下 @Override public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) { // TODO Auto-generated method stub return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup); } //一级标题按下 @Override public void setSelectedGroup(int groupPosition) { // TODO Auto-generated method stub super.setSelectedGroup(groupPosition); }
运行即可。
相关文章推荐
- Android ExpandableListView展开列表控件使用实例
- Android ExpandableListView 展开列表控件(手机QQ好友列表) (二)控件版
- Android ExpandableListView 展开列表控件(手机QQ好友列表)
- Android ExpandableListView每组选一个值,并展开列表
- ExpandableListview控件使用---可展开的列表组件
- Android展开/收缩列表 ExpandableListView 每次只能有一个项目是展开的
- Android ExpandableListView展开时Switch控件错位显示问题
- Android 可展开列表组件 ExpandableListView
- Android::ExpandableListView二级列表控件的使用
- android 中可展开控件ExpandableListView的使用
- Android中可展开的列表组件(ExpandableListView)的使用
- Android 可展开列表组件 ExpandableListView
- android 可展开(收缩)的列表ListView(ExpandableListView)
- Android_QQ好友列表实现---ExpandableListView可展开列表视图
- Android可展开的列表组件(ExpandableListView)模板
- Android 禁止ExpandableListView组的点击展开或隐藏事件,让所有列表都展开
- 仿QQ列表--Android控件ExpandableListView的使用
- Android ExpandableListView列表全部展开与全部收缩
- Android - ExpandableListView的group里添加button后不能展开的解决方法
- Android ListView 列表控件的简单使用