`
swerit
  • 浏览: 57239 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

ExpandableListActivity的使用

 
阅读更多
所用到的布局文件

min.xml(ExpandableListActivity布局文件)

注意事项:

ExpandableListActivity的布局文件中必须包含一个ExpandableListView,并且id必须为="@id/android:list"。还可以包含一个id为empty的TextView,在ExpandableListActivity中没有数据的时候显示该控件的text值。

<LinearLayoutxmlns: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="false">
        </ExpandableListView>
    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="没有数据">
    </TextView>
</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="match_parent"
  android:layout_height="match_parent">
  <TextView android:id="@+id/groupTo"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:paddingLeft="60dip"
     android:paddingTop="10dip"
     android:paddingBottom="10dip"
     android:text="No Date"
    android:textSize="20sp">
  </TextView>
</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="match_parent"
  android:layout_height="match_parent">
  <TextView android:id="@+id/childTo"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:paddingLeft="50dip"
      android:paddingTop="5dip"
      android:paddingBottom="5dip"
      android:textSize="20sp"
      android:text="No Date">
  </TextView>
</LinearLayout>

JAVA代码
package com.test;

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 Sample_ExpandableListActivityActivity 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","第一组");
        Map<String, String> group2=new HashMap<String, String>();
        group2.put("group", "第二组");
        groups.add(group1);
        groups.add(group2);
        //二组条目
        List<List<Map<String, String>>> childs=new ArrayList<List<Map<String,String>>>();
        //第一组二级科目数据
        List<Map<String, String>> child1=new ArrayList<Map<String,String>>();
        Map<String, String> item1=new HashMap<String, String>();
        item1.put("child","科目-1");
        Map<String, String> item2=new HashMap<String, String>();
        item2.put("child","科目-2");
        child1.add(item1);
        child1.add(item2);
        //第二组二级科目数据
        List<Map<String, String>> child2=new ArrayList<Map<String,String>>();
        Map<String, String> item3=new HashMap<String, String>();
        item3.put("child","科目-3");
        Map<String, String> item4=new HashMap<String, String>();
        item4.put("child","科目-4");
        Map<String, String> item5=new HashMap<String, String>();
        item5.put("child","科目-5");
        child2.add(item3);
        child2.add(item4);
        child2.add(item5);
        childs.add(child1);
        childs.add(child2);
        //SimpleExpandableListAdapter构造函数参数
        //1.content
        //2.一级条目数据
        //3.一级条目布局文件
        //4.一级条目Key
        //5.一级条目显示信息控件id
        //6.二级条目数据
        //7.二级条目布局文件
        //8.二级条目Key
        //9.二级条目显示信息控件id
        SimpleExpandableListAdapter adapter=new SimpleExpandableListAdapter(this,groups,R.layout.group,
        new String[]{"group"},new int[]{R.id.groupTo}, childs, R.layout.child, new String[]{"child"},
        new int[]{R.id.childTo});
        setListAdapter(adapter);

    }
}

转:http://blog.163.com/xygzx@126/blog/static/237809502011102010100331/
分享到:
评论

相关推荐

    ExpandableListActivity

    在这个主题下,我们将深入探讨`ExpandableListActivity`的工作原理、使用方法以及相关源码分析。 首先,`ExpandableListActivity`是Android SDK提供的一种便捷方式,用于创建一个包含可展开/折叠组的列表视图。它...

    ExpandableListActivity例子,自动展开

    通过这个例子,开发者可以学习到如何在Android应用中使用`ExpandableListActivity`展示层次结构数据,并通过`Handler`实现动画效果。这在展示具有多级分类的数据时非常有用,例如日历应用的月份和日期、菜单的分类和...

    PreferenceActivity和ExpandableListActivity的使用

    PreferenceActivity和ExpandableListActivity的使用,详细了解请移步:http://blog.csdn.net/zxc514257857/article/details/77773001

    ExpandableListActivity和SimpleExpandableListAdapter的基本使用详解

    ### ExpandableListActivity与SimpleExpandableListAdapter的基本使用详解 在Android开发中,当我们的应用需要显示具有层次结构的数据时,例如家庭成员、公司部门结构等,普通的`ListActivity`就显得力不从心了。这...

    Android学习笔记(二五): 多信息显示-ExpandableListView的使用.doc

    在提供的代码示例中,我们看到`Chapter9Tutorial4`扩展了`ExpandableListActivity`,这是一个专为ExpandableListView设计的Activity基类。在`onCreate`方法中,我们需要设置两个关键的数据结构:`groupData`和`...

    Mars Android视频教程的笔记

    5. **ExpandableListActivity.doc** - ExpandableListView是一种可扩展的列表视图,能展示层级结构的数据。这部分讲述了如何创建和管理子项,以及监听和响应用户的展开和折叠操作。 6. **ContentProvider.doc** - ...

    android学习实例

    Android控件下拉框,单选按钮,复选框,自动补全,日期控件(支持显示格式:年月,年月日,月日),LauncherActivity的使用,ExpandableListActivity实现二级下拉列表,并且在列表项右边加自定义的图片,实现只展开一个菜单的功能...

    Android应用源码之expandableList1_expandableList.zip

    在`ExpandableListActivity.java`或类似的文件中,你需要创建一个自定义的Adapter类,继承自`BaseExpandableListAdapter`,并实现其方法如`getGroupCount()`、`getChildrenCount(int groupPosition)`、`getGroup(int...

    Android-ExpandableListView制作时间轴-TimeLine

    在Android中,我们可以使用`ExpandableListActivity`或者普通的`Activity`配合`ExpandableListView`进行布局。数据通常由`ExpandableListAdapter`管理,这个适配器需要实现`BaseExpandableListAdapter`接口。 在...

    android 的类似于QQ分组的二级列表

    import android.app.ExpandableListActivity; import android.os.Bundle; import android.provider.Contacts.People; import android.util.Log; import android.view.ContextMenu; import android.view.Gravity; ...

    android列表控件实现展开、收缩功能

    在我们的示例代码中,我们创建了一个 ActivityMain 类,该类继承自 ExpandableListActivity,并实现了 onCreate()、onCreateContextMenu() 和 onContextItemSelected() 等方法。这些方法负责初始化列表控件、创建上...

    Android 个人理财工具三:添加账单页面 上

    这里,开发者选择使用ExpandableListActivity而不是Dialog,因为Dialog可能无法满足复杂的交互需求。在账目选择界面中,长按子节点会触发管理账目菜单,如图3所示,用户可以在此添加、删除或修改账目。 为了实现...

    android安卓笔记

    - **ExpandableListActivity-扩展列表页**:`ExpandableListActivity`是一种更为复杂的列表页面,它可以展示具有层次结构的数据。每个顶层项都可以展开显示其子项。 #### TabActivity—标签页 - **TabActivity-标签...

    分享Android中ExpandableListView控件使用教程

    本教程将深入讲解如何在Android应用中使用`ExpandableListView`。 首先,我们需要在布局文件中添加`ExpandableListView`。例如,在 `main.xml` 文件中,我们创建了一个`LinearLayout` 并在其内部嵌入了一个`...

    Android支持展开和收缩功能的列表控件[汇编].pdf

    首先,`ActivityMain`继承自`ExpandableListActivity`,这是一个专门为`ExpandableListView`定制的Activity基类,它简化了扩展列表视图的集成。在`onCreate`方法中,我们设置Activity的标题,并创建了一个`...

    android的ExpandableListView组件.doc

    `ExpandableListView`通常与`ExpandableListActivity`一起使用,就像`ListActivity`与`ListView`的关系一样。 首先,我们需要创建一个包含`ExpandableListView`的布局。在`main.xml`中,你可以看到以下代码: ```...

    ExpandableListView实现手风琴效果

    (2)第二种方法则是创建一个Activity继承自ExpandableListActivity,而后通过getExpandableListView()方法可获得一个ExpandableListView对象。 第二种方法仅适用于一个页面中只有一个ExpandableListView的情况。...

    Android ExpandableListView展开列表控件使用实例

    下面我们将深入探讨如何使用`ExpandableListView`。 首先,`ExpandableListView`的布局文件结构通常包含三个部分:主界面XML(如`main.xml`)、父标题界面XML(如`groups.xml`)和子内容界面XML(如`childs.xml`)...

    Android仿QQ好友列表实现列表收缩与展开

    这时,我们可以使用`ExpandableListView`控件,它提供了多级列表视图,允许用户展开或收起各个组来查看或隐藏子项。本文将详细介绍如何在Android中实现这样一个功能。 首先,要使用`ExpandableListView`,你需要在...

Global site tag (gtag.js) - Google Analytics