在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中設置分組標題了。注意,你需要根據實際情況修改代碼,例如使用自定義的布局文件和數據源。