使用ExpandableListView和ExpandableListAdapter实现分组列表

本文详细介绍了如何使用SimpleExpandableListAdapter与ExpandableListView结合,实现分组列表的功能,并通过三个布局文件展示具体实现过程。通过准备主布局、父列表项布局和子列表项布局,实现数据的分组展示。同时,强调了ExpandableListView与普通ListView的不同之处及setAdapter方法的使用。最后,提供了一个示例代码和效果截图,帮助读者更好地理解和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ExpandableListView直接继承自ListView,而ExpandableListAdapter是一个超级接口,独立于Adapter接口。

ExpandableListAdapter类图:


下面的例子将ExpandableListView与SimpleExpandableListAdapter结合,实现分组列表。

先准备好三个布局文件:

主布局文件expandablelistview_activity.xml,显示ExpandableListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ExpandableListView>

</LinearLayout>

父列表项布局文件expandable_group.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/expandable_group_textview"
        android:layout_marginLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
子列表项布局文件expandable_child.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/expandable_child_textview"
        android:layout_marginLeft="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Activity类:
package com.zzj.ui.expandablelistviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

import com.zzj.ui.R;

public class MainActivity extends Activity implements OnChildClickListener {
	private String[] names = { "腾讯", "百度", "阿里巴巴" };

	private String[][] childnames = { { "QQ", "微信", "手机卫士" },
			{ "百度地图", "百度视频", "PPS&奇艺" }, { "支付宝", "新浪微博", "高德地图" } };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.expandablelistview_activity);
		ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);

		List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

		for (int i = 0; i < names.length; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("name", names[i]);
			groupData.add(map);

			String[] childs = childnames[i];
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			for (int j = 0; j < childs.length; j++) {
				Map<String, String> childMap = new HashMap<String, String>();
				childMap.put("childname", childs[j]);
				list.add(childMap);
			}
			childData.add(list);
		}

		SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
				this, groupData, R.layout.expandable_group,
				new String[] { "name" },
				new int[] { R.id.expandable_group_textview }, childData,
				R.layout.expandable_child, new String[] { "childname" },
				new int[] { R.id.expandable_child_textview });

		expandableListView.setAdapter(adapter);
		expandableListView.setOnChildClickListener(this);
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		String text = names[groupPosition] + "\r\n"
				+ childnames[groupPosition][childPosition];
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
		return true;
	}
}
父列表数据与子列表数据的顺序必须相对应。

给ExpandableListView设置adapter的时候调用的是setAdapter(ExpandableListAdapter adapter)方法,而不是setAdapter(Adapter adapter)方法。setAdapter(Adapter adapter)方法已被重写:

 @Override
    public void setAdapter(ListAdapter adapter) {
        throw new RuntimeException(
                "For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of " +
                "setAdapter(ListAdapter)");
    }

该方法已不允许被调用。

以上例子使用OnChildClickListener对子列表项的点击事件进行监听。

效果图如下:



可以参照SimpleExpandableListAdapter,扩展BaseExpandableListAdapter,实现自己的ExpandableListAdapter。









评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值