在Android中,要设置ExpandableListView的分组标题,需要使用BaseExpandableListAdapter。下面是一个简单的示例来说明如何设置分组标题:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// ...
}
getGroupView()
方法,该方法用于设置分组标题的视图。@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// 获取分组标题
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_group, null);
}
TextView listHeader = (TextView) convertView.findViewById(R.id.list_header);
listHeader.setTypeface(null, Typeface.BOLD);
listHeader.setText(headerTitle);
return convertView;
}
getGroup()
方法中返回分组标题的数据。@Override
public Object getGroup(int groupPosition) {
return headers[groupPosition];
}
ExpandableListView expandableListView = findViewById(R.id.expandableListView);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, headerData, childData);
expandableListView.setAdapter(adapter);
这样,你就可以在ExpandableListView中设置分组标题了。注意,你需要根据实际情况修改代码,例如使用自定义的布局文件和数据源。