android控件之expandableListView

效果是酱紫的!!


main.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <ExpandableListView 
        android:id="@id/android:list"  //这句很重要,没有这句运行会报错
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>



group.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="26sp"
         />
   

</LinearLayout>

child.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
         />
   

</LinearLayout>

mainActivity.java

package com.fzq.expandblelistview;

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

//必须继承ExpandableListActivity
public class MainActivity extends ExpandableListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//创建一个List,该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为第一个一级栏目提供二级栏目数据
		List<Map<String, String>> child1=new ArrayList<Map<String,String>>();
		Map<String, String>childdata1=new HashMap<String, String>();
		childdata1.put("child", "childvalue11");
		child1.add(childdata1);
		Map<String, String>childdata2=new HashMap<String, String>();
		childdata2.put("child", "childvalue12");
		child1.add(childdata2);
		
		//定一个List,该List为第二个一级栏目提供耳机栏目数据
		List<Map<String, String>>  child2=new ArrayList<Map<String,String>>();
	  Map<String, String>  childdata = new HashMap<String, String>();
	  childdata.put("child", "childvalue21");
	  child2.add(childdata);
	  
	  //定一个List,该对象用来存储所有的二级条目数据
	  List<List<Map<String, String>>> childs=new ArrayList<List<Map<String,String>>>();
	  childs.add(child1);
	  childs.add(child2);
	  
	  /*
	   * 
	   * context ->上下文对象
	   * groupData ->一级条目数据
	   * expandedGroupLayout, ->用来设置一级条目样式的布局文件
	   * collapsedGroupLayout,  ->用来指定一级
	   * groupFrom,  ->指定一级条目数据的key
	   * groupTo,    ->  指定一级条目数据显控件的id
	   * childData,   -> 指定二级条目数据
	   * childLayout,  -> 二级条目样式的布局文件
	   * childFrom,  -> 二级条目数据的key
	   * childTo  -> 二级条目数据显示控件的id
	   */
	  
	  SimpleExpandableListAdapter selsAdapter=new SimpleExpandableListAdapter(MainActivity.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(selsAdapter);
	}
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值