ExpandableListView (可展开的列表)组件,用于实现列表的分组显示,点击组item可弹出子item。
ExpandableListView的使用举例:
(1)在Activity的布局文件(activity_main.xml)中定义ExpandableListView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.android_expandable_listview.MainActivity" >
<ExpandableListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" >
</ExpandableListView>
</RelativeLayout>
(2)定义ExpandableListView的组元素布局(expandable_listview_child_item.xml)和子元素(expandable_listview_child_item.xml)布局。
组元素布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView android:id="@+id/group_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:textSize="20sp" >
</TextView>
</RelativeLayout>
子元素布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView android:id="@+id/child_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="15dip" android:textSize="15sp" >
</TextView>
</RelativeLayout>
(3)Activity中的代码:
public class MainActivity extends Activity {
ExpandableListView listview;
List<String> groupData = new ArrayList<String>(); // 组数据
List<List<String>> childData = new ArrayList<List<String>>(); // 子元素数据
MyExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
/** * 初始化数据 */
public void initData() {
/* * 初始化组数据 */
groupData.add("数学");
groupData.add("计算机");
groupData.add("语言");
List<String> child1 = new ArrayList<String>();
child1.add("线性代数");
child1.add("解析几何");
child1.add("概率统计");
List<String> child2 = new ArrayList<String>();
child2.add("数据结构");
child2.add("C语言");
child2.add("Java程序设计");
List<String> child3 = new ArrayList<String>();
child3.add("英语");
child3.add("日语");
child3.add("俄语");
childData.add(child1);
childData.add(child2);
childData.add(child3);
}
/** * 初始化视图 */
public void initView() {
listview = (ExpandableListView) findViewById(R.id.listview);
adapter = new MyExpandableListAdapter(this, groupData, childData);
listview.setAdapter(adapter);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
List<String> groupData;
List<List<String>> childData;
Context context;
public MyExpandableListAdapter(Context context, List<String> groupData, List<List<String>> childData) {
// TODO Auto-generated constructor stub
this.context = context;
this.groupData = groupData;
this.childData = childData;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childData.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
ChildViewHolder childViewHolder = new ChildViewHolder();
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.expandable_listview_child_item, parent,
false);
childViewHolder.childTitle = (TextView) convertView.findViewById(R.id.child_title);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.childTitle.setText(childData.get(groupPosition).get(childPosition));
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childData.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupData.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupData.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GroupViewHolder groupViewHolder = new GroupViewHolder();
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.expandable_listview_group_item, parent,
false);
groupViewHolder.groupTitle = (TextView) convertView.findViewById(R.id.group_title);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.groupTitle.setText(groupData.get(groupPosition));
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
class ChildViewHolder {
TextView childTitle;
}
class GroupViewHolder {
TextView groupTitle;
}
}
}