在Android中,要設(shè)置ExpandableListView的分組標(biāo)題,需要使用BaseExpandableListAdapter。下面是一個(gè)簡單的示例來說明如何設(shè)置分組標(biāo)題:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// ...
}
getGroupView()
方法,該方法用于設(shè)置分組標(biāo)題的視圖。@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// 獲取分組標(biāo)題
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()
方法中返回分組標(biāo)題的數(shù)據(jù)。@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中設(shè)置分組標(biāo)題了。注意,你需要根據(jù)實(shí)際情況修改代碼,例如使用自定義的布局文件和數(shù)據(jù)源。