二级代码

 ?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"
  
  ExpandableListView
   android:id="@+id/ex_list"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:drawSelectorOnTop="false"
   /
  /LinearLayout
   2、一级显示的界面描述文件group.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"
  
  TextView
   android:id="@+id/txt_group"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:paddingLeft="60px"
   android:paddingTop="10px"
   android:paddingBottom="10px"
   android:textSize="26sp"
   android:text="No data"
   /
  /LinearLayout
   3、二级显示的界面描述文件child.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"
  
  TextView
   android:id="@+id/txt_child"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:paddingLeft="50px"
   android:paddingTop="5px"
   android:paddingBottom="5px"
   android:textSize="20sp"
   android:text="No data"
   /
  /LinearLayout
   4、编写活动程序Main.java
   package com.yazhou.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 Main extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   // 获得二级列表的控件
   ExpandableListView expandable_listview = (ExpandableListView) findViewById(R.id.ex_list);
   // 为一级显示添加数据
   List<MAP> list_group = new ArrayList<MAP>();
   for (int i = 0; i < 2; i++) {
   Map map_group = new HashMap();
   map_group.put("group", "group" + i);
   list_group.add(map_group);
   }
   // 为一级显示的条目一添加二级数据
   List<MAP> list_child1 = new ArrayList<MAP>();
   for (int i = 0; i < 2; i++) {
   Map map_child1 = new HashMap();
   map_child1.put("child", "child" + i);
   list_child1.add(map_child1);
   }
   // 为一级显示的条目二添加二级数据
   List<MAP> list_child2 = new ArrayList<MAP>();
   Map map_child2 = new HashMap();
   map_child2.put("child", "child2");
   list_child2.add(map_child2);
   // 把二级数据添加到集合
   List<LIST<MAP>> list_childs = new ArrayList<LIST<MAP>>();
   list_childs.add(list_child1);
   list_childs.add(list_child2);
   // 把一级数据和二级数据绑定到SimpleExpandableListAdapter对象
   SimpleExpandableListAdapter expandable_listadapter = new SimpleExpandableListAdapter(
   Main.this, list_group, R.layout.group,
   new String[] { "group" }, new int[] { R.id.txt_group },
   list_childs, R.layout.child, new String[] { "child" },
   new int[] { R.id.txt_child });
   //将SimpleExpandableListAdapter对象设置到二级列表控件
   expandable_listview.setAdapter(expandable_listadapter);
   }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值