ExpandableListView,就是可折叠的列表,它是ListView的子类, 在ListView的基础上它把应用中的列表项分为几组,每组里又可包含多个列表项。
ExpandableListView用法和Listview差不多,设置适配器,适配器里面设置数据。
1.我们先在控制器的布局文件中添加一个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:padding="5dp"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/exlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:childDivider="#E02D2F"/>
</RelativeLayout>
2.ExpandableListView中分为组头和组里的每一个item,所以需要两个布局文件,相互对应。
设置组头的布局文件group_item.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">
<TextView
android:id="@+id/tv_group_name"
android:layout_width="match_parent"
android:layout_height="56dp"
android:gravity="center_vertical"
android:paddingLeft="30dp"
android:text="组头"
android:textStyle="bold"
android:textSize="20sp" />
</LinearLayout>
设置每一组的item布局文件list_item.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">
<ImageView
android:id="@+id/img_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@mipmap/ic_launcher"
android:focusable="false"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:focusable="false"
android:text="每一组"
android:textSize="18sp" />
</LinearLayout>
回到控制器中
获取到ExpandableListView
expandableListView = (ExpandableListView) findViewById(R.id.exlistview);
设置适配器
expandableListView.setAdapter(new MyExpandableListView());// 设置适配器
自定义适配器
//为ExpandableListView自定义适配器
class MyExpandableListView extends BaseExpandableListAdapter {
//返回一级列表的个数
@Override
public int getGroupCount() {
return groups.length;
}
//返回每个二级列表的个数
@Override
public int getChildrenCount(int groupPosition) { //参数groupPosition表示第几个一级列表
ArrayList list = childs.get(groupPosition);
return list.size();
// return childs[groupPosition].length;
}
//返回一级列表的单个item(返回的是对象)
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
//返回二级列表中的单个item(返回的是对象)
@Override
public Object getChild(int groupPosition, int childPosition) {
// return childs[groupPosition][childPosition]; //不要误写成groups[groupPosition][childPosition]
ArrayList list = childs.get(groupPosition);
return list.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//每个item的id是否是固定?一般为true
@Override
public boolean hasStableIds() {
return true;
}
//【重要】填充一级列表
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.group_item, null);
} else {
}
TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group_name);
tv_group.setText(groups[groupPosition]);
return convertView;
}
//【重要】填充二级列表
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, null);
}
ImageView iv_child = (ImageView) convertView.findViewById(R.id.img_icon);
TextView tv_child = (TextView) convertView.findViewById(R.id.tv_name);
ArrayList list = childs.get(groupPosition);
ItemModel model = (ItemModel)list.get(childPosition);
iv_child.setImageResource(model.getiId());
tv_child.setText(model.getiName());
// tv_child.setText(childs[groupPosition][childPosition]);
return convertView;
}
//二级列表中的item是否能够被选中?
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
设置每一组是否展开:.expandGroup()
全部展开:
for (int i = 0;i<groups.length;
expandableListView.expandGroup(i);// 设置展开
}
// 设置组头箭头为空
expandableListView.setGroupIndicator(null);
网友评论