加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Android入门之ExpandableListView控件的实施

发布时间:2021-11-25 17:12:35 所属栏目:PHP教程 来源:互联网
导读:本文通过学习ExpandableListView控件的使用,同时学习ExpandableListActivity类和SimpleExpandableListAdapter适配器的使用。 通过例子讲解,代码中有详细的注释: 首先layout中有3个布局文件: main.xml ?xml version=1.0 encoding=utf-8? LinearLayout xml

本文通过学习ExpandableListView控件的使用,同时学习ExpandableListActivity类和SimpleExpandableListAdapter适配器的使用。
 
通过例子讲解,代码中有详细的注释:
 
首先layout中有3个布局文件:
 
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"
    >
    <ExpandableListView
     android:id="@id/android:list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:drawSelectorOnTop="true"
    /><!-- android:drawSelectorOnTop:是否高亮显示 -->
 <TextView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:text="no data"
     android:id="@id/android:empty"
    />
</LinearLayout>
 
---------------------------------------------------------------------------
 
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:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:text="no data"
     android:id="@+id/tvgroup"
     android:paddingBottom="10px"
     android:paddingLeft="50px"
    
    />
</LinearLayout>
 
--------------------------------------------------
 
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:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="no data"
     android:id="@+id/tvchild"
     android:paddingLeft="50px"
    />
</LinearLayout>
 
---------------------------------------------------------------------
 
java代码:
 
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;
 
public class ExpandableListActivityTest extends ExpandableListActivity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
  Map<String, String> group1 = new HashMap<String, String>();
  group1.put("group", "huangweiyong");
 
  Map<String, String> group2 = new HashMap<String, String>();
  group2.put("group", "xiaoqi");
 
  Map<String, String> group3 = new HashMap<String, String>();
  group3.put("group", "hahaha");
 
  groups.add(group1);
  groups.add(group2);
  groups.add(group3);
 
  List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
  Map<String, String> child1Data1 = new HashMap<String, String>();
  child1Data1.put("child", "child1Data1");
  Map<String,String> child1Data2 = new HashMap<String,String>();
  child1Data2.put("child", "child1Data2");
  child1.add(child1Data1);
  child1.add(child1Data2);
 
  List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
  Map<String, String> child2Data1 = new HashMap<String, String>();
  child2Data1.put("child", "child1Data1");
  child2.add(child2Data1);
 
  List<Map<String, String>> child3 = new ArrayList<Map<String, String>>();
  Map<String, String> child3Data1 = new HashMap<String, String>();
  child3Data1.put("child", "child1Data1");
  child3.add(child3Data1);
 
  List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
  childs.add(child1);
  childs.add(child2);
  childs.add(child3);
 
  /**
   * 生成一个SimpleExpandableListAdapter对象
   * 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.tvgroup }, childs, R.layout.child,
    new String[] { "child" }, new int[] { R.id.tvchild });
  //将适配器对象设置给ExpandableListActivity
  setListAdapter(sela);
 }
}

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读