ExpandableListView是Android平台提供的一个可展开的列表视图,它可以显示分组及其子项的数据。ExpandableListView继承自ListView,并提供了添加分组和子项、展开和折叠分组等功能。
使用ExpandableListView时,你需要使用一个适配器(Adapter)来提供数据,并根据数据的组和子项数量来配置列表视图。
下面是一个简单的示例,展示如何使用ExpandableListView:
private String [] groups = {"A","B","C"};
private String[][] childs ={ {"A1","A2"},{"B1"},{"C1","C2","C3"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView view = findViewById(R.id.myExpandableListViewId);
MyExpandableListAdapter myExpandableListAdapter = new MyExpandableListAdapter();
view.setAdapter(myExpandableListAdapter);
//设置默认展开分组
view.expandGroup(0);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return childs[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs[groupPosition][childPosition];
}
/**此方法用于返回具体item的id,一般直接返回position*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//是否有稳定的ID
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//获取item view 并设置显示格式
TextView textView = (TextView) LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);
textView.setText(groups[groupPosition]);
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = (TextView) LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);
textView.setText(childs[groupPosition][childPosition]);
return textView;
}
//是否可以点击
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void onGroupExpanded(int groupPosition) {
}
@Override
public void onGroupCollapsed(int groupPosition) {
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
}
界面布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myExpandableListViewId"
>
</ExpandableListView>
</androidx.constraintlayout.widget.ConstraintLayout>