溫馨提示×

ExpandableListView分組標題如何設置

小樊
94
2024-09-04 07:36:36
欄目: 編程語言

在Android中,要設置ExpandableListView的分組標題,需要使用BaseExpandableListAdapter。下面是一個簡單的示例來說明如何設置分組標題:

  1. 首先,創建一個ExpandableListView的適配器,繼承自BaseExpandableListAdapter。
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    // ...
}
  1. 實現適配器中的方法,特別是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;
}
  1. getGroup()方法中返回分組標題的數據。
@Override
public Object getGroup(int groupPosition) {
    return headers[groupPosition];
}
  1. 在你的Activity或Fragment中,初始化ExpandableListView并設置適配器。
ExpandableListView expandableListView = findViewById(R.id.expandableListView);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, headerData, childData);
expandableListView.setAdapter(adapter);

這樣,你就可以在ExpandableListView中設置分組標題了。注意,你需要根據實際情況修改代碼,例如使用自定義的布局文件和數據源。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女