Android ExpandableListView
Android ExpandableListView
控件的使用
<?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:id="@+id/expand_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
首先,添加ExpandableListView
到主页面中。
数据源设置
//用于 Item Group 数据源
private List<String> lsGroupData = new ArrayList<>();
//用于 Item Child 数据源
private List<List<String>> lsChildGroup = new ArrayList<>();
// 初始换数据
private void initData() {
int groupCount = 10;
for (int i = 0; i < groupCount; i++) {
lsGroupData.add("Item - " + i);
int childCount = new Random().nextInt(30);
List<String> lsChildData = new ArrayList<>();
for (int i1 = 0; i1 < childCount; i1++) {
lsChildData.add("item - " + i + " - " + i1);
}
lsChildGroup.add(lsChildData);
}
}
适配器 代码
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> lsGroupData;
private List<List<String>> lsChildData;
/**
* 构造函数
* @param context
* @param lsGroupData 存储 关于 Item Group的全部数据
* @param lsChildData 存储关于 Item Child的全部数据
*/
MyAdapter(Context context, List<String> lsGroupData, List<List<String>> lsChildData) {
this.context = context;
this.lsGroupData = lsGroupData;
this.lsChildData = lsChildData;
}
/**
* 获取 Group 大小
* @return
*/
@Override
public int getGroupCount() {
return lsGroupData.size();
}
/**
* 获取 Child 大小
* @param groupPosition
* @return
*/
@Override
public int getChildrenCount(int groupPosition) {
return lsChildData.get(groupPosition).size();
}
/**
* 获取 指定 Position 下的Group 数据
* @param groupPosition
* @return
*/
@Override
public Object getGroup(int groupPosition) {
return lsGroupData.get(groupPosition);
}
/**
* 获取 指定 Position 下的Child 数据
* @param groupPosition
* @param childPosition
* @return
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return lsChildData.get(groupPosition).get(childPosition);
}
/**
* 获取Group ID
* @param groupPosition
* @return
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* 获取Child ID
* @param groupPosition
* @param childPosition
* @return
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 具有稳定的ID
* @return
*/
@Override
public boolean hasStableIds() {
return false;
}
/**
* 获取 Group 项的 View
* @param groupPosition
* @param isExpanded
* @param convertView
* @param parent
* @return
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_group, null);
}
TextView txtGroupName = convertView.findViewById(R.id.txtGroupName);
txtGroupName.setText(lsGroupData.get(groupPosition));
TextView txtItemCount = convertView.findViewById(R.id.txtItemsCount);
txtItemCount.setText(String.valueOf(lsChildData.get(groupPosition).size()));
return convertView;
}
/**
* 获取 Child 项的 View
* @param groupPosition
* @param childPosition
* @param isLastChild
* @param convertView
* @param parent
* @return
*/
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_child, null);
}
TextView txtChildName = convertView.findViewById(R.id.txtItemName);
txtChildName.setText(lsChildData.get(groupPosition).get(childPosition));
txtChildName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, lsChildData.get(groupPosition).get(childPosition), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这部分就是适配器代码。
我们来分析一下:
首先,我们将我们的适配器类继承与BaseExpandableListAdapter
然后重写如下方法:
我们重写下面所有的方法。
相关的方法注释在上面的代码中由具体写到。
最后,我们来看一下效果图:
这样,ExpandableListView
的使用就到这里了。