ExpandableListView的多級(jí)展開

小樊
81
2024-10-14 13:41:06
欄目: 編程語言

ExpandableListView 是 Android 中用于實(shí)現(xiàn)多級(jí)展開列表的組件。它允許用戶點(diǎn)擊列表項(xiàng)以展開或折疊子列表項(xiàng)。要實(shí)現(xiàn)多級(jí)展開,你需要為每個(gè)父列表項(xiàng)創(chuàng)建一個(gè)子列表項(xiàng)集合,并在點(diǎn)擊父列表項(xiàng)時(shí)更新子列表項(xiàng)的可見性。

以下是一個(gè)簡單的示例,展示了如何使用 ExpandableListView 實(shí)現(xiàn)多級(jí)展開列表:

  1. 首先,在布局文件中添加 ExpandableListView
<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
  1. 創(chuàng)建一個(gè)數(shù)據(jù)源類,用于存儲(chǔ)每一級(jí)列表項(xiàng)的信息:
public class DataSource {
    private List<String> groupHeaders;
    private Map<String, List<String>> childItems;

    public DataSource() {
        groupHeaders = new ArrayList<>();
        childItems = new HashMap<>();
    }

    public void addGroup(String header) {
        groupHeaders.add(header);
    }

    public void addChild(String header, List<String> items) {
        childItems.put(header, items);
    }

    public List<String> getGroupHeaders() {
        return groupHeaders;
    }

    public Map<String, List<String>> getChildItems() {
        return childItems;
    }
}
  1. 創(chuàng)建一個(gè)自定義的 BaseExpandableListAdapter,用于綁定數(shù)據(jù)源和顯示列表項(xiàng):
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private DataSource dataSource;

    public CustomExpandableListAdapter(Context context, DataSource dataSource) {
        this.context = context;
        this.dataSource = dataSource;
    }

    @Override
    public int getGroupCount() {
        return dataSource.getGroupHeaders().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return dataSource.getChildItems().get(dataSource.getGroupHeaders().get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return dataSource.getGroupHeaders().get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return dataSource.getChildItems().get(dataSource.getGroupHeaders().get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        // 在這里創(chuàng)建和設(shè)置父列表項(xiàng)的視圖
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        // 在這里創(chuàng)建和設(shè)置子列表項(xiàng)的視圖
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  1. 在 Activity 或 Fragment 中設(shè)置 ExpandableListView 的適配器,并處理點(diǎn)擊事件以展開或折疊子列表項(xiàng):
public class MainActivity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    private CustomExpandableListAdapter adapter;
    private DataSource dataSource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        expandableListView = findViewById(R.id.expandableListView);

        dataSource = new DataSource();
        dataSource.addGroup("Group 1");
        dataSource.addGroup("Group 2");

        List<String> group1Items = new ArrayList<>();
        group1Items.add("Item 1.1");
        group1Items.add("Item 1.2");
        dataSource.addChild("Group 1", group1Items);

        List<String> group2Items = new ArrayList<>();
        group2Items.add("Item 2.1");
        group2Items.add("Item 2.2");
        dataSource.addChild("Group 2", group2Items);

        adapter = new CustomExpandableListAdapter(this, dataSource);
        expandableListView.setAdapter(adapter);

        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                boolean isExpanded = expandableListView.isGroupExpanded(groupPosition);
                expandableListView.setGroupExpanded(groupPosition, !isExpanded);
                return true;
            }
        });
    }
}

這個(gè)示例展示了如何使用 ExpandableListView 實(shí)現(xiàn)多級(jí)展開列表。你可以根據(jù)需要修改數(shù)據(jù)源類和自定義適配器以滿足你的需求。

0