android 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"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <!-- ExpandableListView的 android:layout_height,如果parent的大小不是固定大小的话最好不用用warp_content -->
    <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000"
        android:text="No data" />

</LinearLayout>

public class MainActivity extends ExpandableListActivity {

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

		List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
		Map<String, String> group1 = new HashMap<String, String>();
		group1.put("group", "中国");
		Map<String, String> group2 = new HashMap<String, String>();
		group2.put("group", "美国");
		groups.add(group1);
		groups.add(group2);

		// 定义一个List,该List对象为第一个一级条目提供二级条目的数据
		List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
		Map<String, String> child1Data1 = new HashMap<String, String>();
		child1Data1.put("child", "北京");
		child1.add(child1Data1);
		Map<String, String> child1Data2 = new HashMap<String, String>();
		child1Data2.put("child", "上海");
		child1.add(child1Data2);

		// 定义一个List,该List对象为第二个一级条目提供二级条目的数据
		List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
		Map<String, String> child2Data = new HashMap<String, String>();
		child2Data.put("child", "华盛顿");
		child2.add(child2Data);
		Map<String, String> child2Data2 = new HashMap<String, String>();
		child2Data2.put("child", "纽约");
		child2.add(child2Data2);

		// 定义一个List,该List对象用来存储所有的二级条目的数据
		List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
		childs.add(child1);
		childs.add(child2);

		// 生成一个SimpleExpandableListAdapter对象
		// 1.context
		// 2.一级条目的数据
		// 3.用来设置一级条目样式的布局文件
		// 4.指定一级条目数据的key
		// 5.指定一级条目数据显示控件的id
		// 6.指定二级条目的数据
		// 7.用来设置二级条目样式的布局文件
		// 8.指定二级条目数据的key
		// 9.指定二级条目数据显示控件的id
		// 其中android.R.layout.simple_expandable_list_item_1、android.R.id.text1都是android本身提供的,
		// 所以如果对默认的布局不满意的话,可以自己创建好看的布局,把layout和layout里的TextView替换就行了
		SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
				this, groups, android.R.layout.simple_expandable_list_item_1,
				new String[] { "group" }, new int[] { android.R.id.text1 },
				childs, android.R.layout.simple_expandable_list_item_2,
				new String[] { "child" }, new int[] { android.R.id.text2 });
		// 将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity
		setListAdapter(sela);
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值