ExpandableListView

ExpandableListView的多级展开

小樊
84
2024-10-14 13:41:06
栏目: 编程语言

ExpandableListView 是 Android 中用于实现多级展开列表的组件。它允许用户点击列表项以展开或折叠子列表项。要实现多级展开,你需要为每个父列表项创建一个子列表项集合,并在点击父列表项时更新子列表项的可见性。

以下是一个简单的示例,展示了如何使用 ExpandableListView 实现多级展开列表:

  1. 首先,在布局文件中添加 ExpandableListView
<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
  1. 创建一个数据源类,用于存储每一级列表项的信息:
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. 创建一个自定义的 BaseExpandableListAdapter,用于绑定数据源和显示列表项:
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) {
        // 在这里创建和设置父列表项的视图
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        // 在这里创建和设置子列表项的视图
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  1. 在 Activity 或 Fragment 中设置 ExpandableListView 的适配器,并处理点击事件以展开或折叠子列表项:
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;
            }
        });
    }
}

这个示例展示了如何使用 ExpandableListView 实现多级展开列表。你可以根据需要修改数据源类和自定义适配器以满足你的需求。

0
看了该问题的人还看了