Android学习笔记(三十)

ExpandableListView

 

ExpandableListViewActivity

package org.wp.activity;

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.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class ExpandableListViewActivity extends Activity {
	private ExpandableListView expandableListView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		expandableListView = (ExpandableListView) this
				.findViewById(R.id.expandableListViewId);

		// 一级条目的数据
		List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
		Map<String, String> group1 = new HashMap<String, String>();
		group1.put("week", "星期一");
		Map<String, String> group2 = new HashMap<String, String>();
		group2.put("week", "星期二");
		groups.add(group1);
		groups.add(group2);

		// 二级条目的数据
		List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

		// 第一个一级条目所包含的二级条目数据
		List<Map<String, String>> group1Child = new ArrayList<Map<String, String>>();
		Map<String, String> group1ChindData1 = new HashMap<String, String>();
		group1ChindData1.put("course", "英语");
		Map<String, String> group1ChindData2 = new HashMap<String, String>();
		group1ChindData2.put("course", "数学");
		Map<String, String> group1ChindData3 = new HashMap<String, String>();
		group1ChindData3.put("course", "语文");
		group1Child.add(group1ChindData1);
		group1Child.add(group1ChindData2);
		group1Child.add(group1ChindData3);

		// 第二个一级条目所包含的二级条目数据
		List<Map<String, String>> group2Child = new ArrayList<Map<String, String>>();
		Map<String, String> group2ChildData1 = new HashMap<String, String>();
		group2ChildData1.put("course", "语文");
		Map<String, String> group2ChildData2 = new HashMap<String, String>();
		group2ChildData2.put("course", "化学");
		Map<String, String> group2ChildData3 = new HashMap<String, String>();
		group2ChildData3.put("course", "物理");
		group2Child.add(group2ChildData1);
		group2Child.add(group2ChildData2);
		group2Child.add(group2ChildData3);

		childs.add(group1Child);
		childs.add(group2Child);

		SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
				this, groups, R.layout.group, new String[] { "week" },
				new int[] { R.id.groupView }, childs, R.layout.child,
				new String[] { "course" }, new int[] { R.id.childView });
		expandableListView.setAdapter(sela);
	}
}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
   	android:layout_width="fill_parent"
   	android:layout_height="fill_parent"
    >
   	<!-- 
    	android:drawSelectorOnTop	
    	setDrawSelectorOnTop(boolean) 	
    	When set to true, the selector will be drawn over the selected item.  
    -->
	<ExpandableListView
		android:id="@+id/expandableListViewId"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:drawSelectorOnTop="false"
		/>
	<!-- 
		@id/android:empty
		当ExpandableListView里面没有条目时在这个布局里面
		这个TextView将显示 
	-->	
	<TextView
		android:id="@id/android:empty"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:text="no data"
		/>		
</LinearLayout>

 

group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	>
  	<TextView
  		android:id="@+id/groupView"
  		android:layout_width="fill_parent"
  		android:layout_height="fill_parent"
  		android:paddingLeft="60px"
  		android:paddingTop="10px"
  		android:paddingBottom="10px"
  		android:textSize="26sp"
  		android:text="no data"
  		/>
</LinearLayout>

 

child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  	android:layout_width="fill_parent"
 	android:layout_height="fill_parent"
 	>
 	<TextView
 		android:id="@+id/childView"
 		android:layout_width="fill_parent"
 		android:layout_height="fill_parent"
 		android:paddingLeft="50px"
 		android:paddingTop="5px"
 		android:paddingBottom="5px"
 		android:textSize="20sp"
 		android:text="no data"
 		/>
</LinearLayout>

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值