ExpandableListActivity的demo——自练

看着Mars 老师的视频,自己练习了一下。感谢Mars老师,感谢帮助过我的人,其实一直在看别人写的文章吐舌头,今天我也写一下,必备忘记时用。

这两张图片即这个小程序的效果图。红色框的是第一级条目,绿色框是第二级条目。第一个一级条目 group1 有两个子条目(即——child1、child2),第二个一级条目 group2有一个二级条目(即g2Child)。



这个Activity继承的是ExpandableListActivity,使用的适配器是SimpleExpandableListAdapter

SimpleExpandableListAdapter sela =
new SimpleExpandableListAdapter(this, groups, R.layout.group,
new String[]{"group"}, new int[]{R.id.group}, childs,
R.layout.child, new String[]{"child"}, new int[]{R.id.child});
setListAdapter(sela);

package com.example.expandablelistactivity1;

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

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.SimpleExpandableListAdapter;

public class MainActivity extends ExpandableListActivity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//定义一个List,为一级条目提供数据
		List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
		Map<String, String > group1 = new HashMap<String, String>();
		group1.put("group", "group1");
		Map<String, String > group2 = new HashMap<String, String>();
		group2.put("group", "group2");
		groups.add(group1);
		groups.add(group2);
		
		//定义了一个list,为第一个一级条目提供二级条目的数据
		List<Map<String, String>> group1Child = new ArrayList<Map<String, String>>();
		Map<String, String> child1 = new HashMap<String , String>();
		child1.put("child", "child1");
		Map<String, String> child2 = new HashMap<String , String>();
		child2.put("child", "child2");
		group1Child.add(child1);
		group1Child.add(child2);
		
		//定义了一个list ,为第二个一级条目提供二级条目的数据
		List<Map<String , String>> group2Child = new ArrayList<Map<String,String>>();
		Map<String , String> group2ChildData = new HashMap<String, String>();
		group2ChildData.put("child", "g2Child");
		group2Child.add(group2ChildData);
		
		List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
		childs.add(group1Child);
		childs.add(group2Child);
		
		/**
		 * 1.Context
		 * 2.一级条目的数据
		 * 3.设置一级条目样式的布局文件
		 * 4.指定一级条目数据的key
		 * 5.指定一级条目数据的id
		 * 6.指定二级条目的数据
		 * 7.设置二级条目样式的布局文件		 * 
		 * 8.指定二级条目数据的key
		 * 9.指定二级条目数据的id
		 */
		SimpleExpandableListAdapter sela = 
				new SimpleExpandableListAdapter(this, groups, R.layout.group,
				new String[]{"group"},  new int[]{R.id.group}, childs, 
				R.layout.child, new String[]{"child"}, new int[]{R.id.child});
		setListAdapter(sela);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

使用的布局文件:

1.activity_main.xml—— 主布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="no date" />

    <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@id/android:empty"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="66dp" >
    </ExpandableListView>

</RelativeLayout>

2. group.xml—— 一级条目布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="no data"
        android:layout_marginLeft="30dp" />

   

</RelativeLayout>

3.child.xml———二级条目的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="no child data" 
        android:layout_marginLeft="5dp"/>


</RelativeLayout>




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值