ExpandableListView葵诈,就是可折疊的列表,它是ListView的子類麸俘, 在ListView的基礎(chǔ)上它把應(yīng)用中的列表項(xiàng)分為幾組齐板,每組里又可包含多個(gè)列表項(xiàng)吵瞻。
ExpandableListView用法和Listview差不多葛菇,設(shè)置適配器,適配器里面設(shè)置數(shù)據(jù)橡羞。
1.我們先在控制器的布局文件中添加一個(gè)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中分為組頭和組里的每一個(gè)item眯停,所以需要兩個(gè)布局文件,相互對(duì)應(yīng)卿泽。
設(shè)置組頭的布局文件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>
設(shè)置每一組的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);
設(shè)置適配器
expandableListView.setAdapter(new MyExpandableListView());// 設(shè)置適配器
自定義適配器
//為ExpandableListView自定義適配器
class MyExpandableListView extends BaseExpandableListAdapter {
//返回一級(jí)列表的個(gè)數(shù)
@Override
public int getGroupCount() {
return groups.length;
}
//返回每個(gè)二級(jí)列表的個(gè)數(shù)
@Override
public int getChildrenCount(int groupPosition) { //參數(shù)groupPosition表示第幾個(gè)一級(jí)列表
ArrayList list = childs.get(groupPosition);
return list.size();
// return childs[groupPosition].length;
}
//返回一級(jí)列表的單個(gè)item(返回的是對(duì)象)
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
//返回二級(jí)列表中的單個(gè)item(返回的是對(duì)象)
@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;
}
//每個(gè)item的id是否是固定莺债?一般為true
@Override
public boolean hasStableIds() {
return true;
}
//【重要】填充一級(jí)列表
@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;
}
//【重要】填充二級(jí)列表
@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;
}
//二級(jí)列表中的item是否能夠被選中?
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
設(shè)置每一組是否展開:.expandGroup()
全部展開:
for (int i = 0;i<groups.length;
expandableListView.expandGroup(i);// 設(shè)置展開
}
// 設(shè)置組頭箭頭為空
expandableListView.setGroupIndicator(null);