在Android中,要设置ExpandableListView的分组标题,需要使用BaseExpandableListAdapter。下面是一个简单的示例来说明如何设置分组标题:
首先,创建一个ExpandableListView的适配器,继承自BaseExpandableListAdapter。public class MyExpandableListAdapter extends BaseExpandableListAdapter { // ...}实现适配器中的方法,特别是getGroupView()方法,该方法用于设置分组标题的视图。@Overridepublic 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()方法中返回分组标题的数据。@Overridepublic Object getGroup(int groupPosition) { return headers[groupPosition];}在你的Activity或Fragment中,初始化ExpandableListView并设置适配器。ExpandableListView expandableListView = findViewById(R.id.expandableListView);MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, headerData, childData);expandableListView.setAdapter(adapter);这样,你就可以在ExpandableListView中设置分组标题了。注意,你需要根据实际情况修改代码,例如使用自定义的布局文件和数据源。


