Android中可展开的列表组件(ExpandableListView)的使用
2013-06-14 23:58
686 查看
可展开的列表组件(ExpandableListView)是ListView的子类,它在普通ListView的基础上进行了扩展,把列表项分为几组,每组又可包含多个列表项。ExpandableListView和ListView的用法非常相似,只是列表项应该由ExpandableAdapter提供,下表给出了它所支持的额外XML属性:
下面通过一个简单实例来演示ExpandableListView的使用,需要在组列表和子列表添加图片等组件就自行添加,代码如下:
Activity:
布局XML:
附上图片效果:
展开子列表:
下面通过一个简单实例来演示ExpandableListView的使用,需要在组列表和子列表添加图片等组件就自行添加,代码如下:
Activity:
package com.lovo; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 创建一个可展开的列表组件ExpandableListAdapter对象 ExpandableListAdapter adapter = new BaseExpandableListAdapter() { // 添加图片 // int[] logos = new int[] { R.drawable.image1, // R.drawable.image2, R.drawable.image2 }; // 每一个列表的字符数组 private String[] roleTypes = new String[] { "圣职者", "神枪手", "鬼剑士" }; // 每个列表下面的子列表字符数组 private String[][] roles = new String[][] { { "蓝拳", "驱魔", "圣骑士", "复仇者" }, { "漫游", "大枪", "弹药", "机械" }, { "剑魂", "红眼", "阿修罗", "鬼泣" } }; @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } // 构建每个父列表组件 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // 创建一个LinearLayout对象 LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(0); // ImageView logo = new ImageView(MainActivity.this); // // logo.setImageResource(logos[groupPosition]); // ll.addView(logo); TextView textView = getTextView(); // 根据groupPosition取roleTypes对应文本作为textView显示的文本内容 textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public int getGroupCount() { return roleTypes.length; } @Override public Object getGroup(int groupPosition) { return roleTypes[groupPosition]; } @Override public int getChildrenCount(int groupPosition) { return roles[groupPosition].length; } /** * 在MainActivity里创建TextView组件 * * @return TextView */ private TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); return textView; } // 构建子列表的数据 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); // 根据groupPosition和childPosition取roles中对应文本作为textView显示的内容 textView.setText(getChild(groupPosition, childPosition) .toString()); return textView; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public Object getChild(int groupPosition, int childPosition) { return roles[groupPosition][childPosition]; } }; // 获得ExpandableListView对象 ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list); // 为ExpandableListView对象设置适配器 expandableListView.setAdapter(adapter); } }
布局XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" > </ExpandableListView> </LinearLayout>
附上图片效果:
展开子列表:
相关文章推荐
- Android ExpandableListView展开列表控件使用实例
- Android 可展开列表组件 ExpandableListView
- ExpandableListView(可展开的列表组件)使用方法
- Android 可展开列表组件 ExpandableListView
- 一看就会Android之分组列表组件ExpandableListView的使用
- Android可展开的列表组件(ExpandableListView)模板
- ExpandableListview控件使用---可展开的列表组件
- Android列表组件ListView使用详解之设置显示第几项子项
- android 扩展列表ExpandableListView的详解和使用
- android 可展开(收缩)的列表ListView(ExpandableListView)
- android--------ExpandableListView的使用多级列表
- Android 仿QQ好友分组列表、ExpandableListView的使用详解
- Android基础入门教程——2.4.12 ExpandableListView(可折叠列表)的基本使用
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- 可展开的列表组件ExpandableListView
- ExpandableListView(可展开的列表组件)的说明以及其用法
- android_ExpandableListView 下拉列表使用
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- ExpandableListView--可展开的列表组件