`

ExpandableListActivity和SimpleExpandableListAdapter的使用方法

阅读更多
下面通过我自己的学习来总结一下,ExpandableListActivity和SimpleExpandableListAdapter的使用。当我们想要给客户展示一组数据(这里我们之前是使用ListActivity),但数据里面又进行了分组,这就不能使用ListActivity,就要用我们下面要讲的ExpandableListActivity。
那请问:ExpandableListActivity和SimpleExpandableListAdapter两者有什么关系了?这就好像ListActivity和SimpleListAdapter一样,SimpleExpandableListAdapter为ExpandableListActivity提供数据。好的,下面就来讲解ExpandableListActivity和SimpleExpandableListAdapte的使用步骤:
1.在布局文件当中声明ExpandableActivity控件:
Main.xml:
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data"
/>

2.在layout文件夹当中,新建group.xml,为一级条目声明样式
Gruop.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/groupto"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingTop="10px"
  android:paddingLeft="60px"
  android:paddingBottom="10px"
  android:textSize="26sp"
  android:text="No data"
  />
</LinearLayout>
3.在layout文件夹当中,新建child.xml,为二级条目声明样式
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/childto"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingTop="5px"
  android:paddingLeft="50px"
  android:paddingBottom="5px"
  android:textSize="20sp"
  android:text="No data"
  />
</LinearLayout>
4.创建一个Activity,继承ExpandableListActivity:
代码具体如下:
MainActivity.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;

/*
* 创建一个Activity,继承ExpandableListAcitivty
*/
public class MainActivity extends ExpandableListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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> child1Data1 = new HashMap<String, String>();
child1Data1.put("child", "child1Data1");
child1.add(child1Data1);
Map<String,String> child1Data2 = new HashMap<String,String>();
child1Data2.put("child", "child1Data2");
child1.add(child1Data2);

//定义一个List,该List对象为第二个一级条目提供二级条目的数据
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2Data = new HashMap<String, String>();
child2Data.put("child", "child2Data");
child2.add(child2Data);

//定义一个List,该List对象用来存储所有的二级条目的数据
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(child1);
childs.add(child2);

//生成一个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.groupTo }, childs, R.layout.child,
new String[] { "child" }, new int[] { R.id.childTo });
//将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity
setListAdapter(sela);
}
}

分享到:
评论

相关推荐

    ExpandableListActivity和SimpleExpandableListAdapter的基本使用详解

    通过以上步骤,我们就完成了使用`ExpandableListActivity`和`SimpleExpandableListAdapter`展示层次数据的过程。这种组合不仅提供了丰富的用户体验,还大大简化了开发流程,使开发者能够更加专注于应用程序的核心...

    ExpandableListActivity

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

    ExpandableListActivity例子,自动展开

    在`ExpandableListActivity`的`onCreate()`方法中,我们需要实例化适配器并设置到`ExpandableListView`,像这样: ```java ExpandableListAdapter adapter = new MyExpandableListAdapter(this, groups, children...

    PreferenceActivity和ExpandableListActivity的使用

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

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

    在Android中,我们可以使用`SimpleExpandableListAdapter`这个现成的适配器,它接受HashMap数据源并简化了数据绑定过程。适配器的构造函数需要提供Group和Child的布局文件ID,以及Group和Child数据的映射列表。在这...

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

    在Android应用开发中,`ExpandableListView`是一个非常有用的控件,它允许用户展示一个可折叠/展开的列表,这种列表通常用于显示...这个例子展示了基础的使用方式,但实际项目中可能需要根据需求进行更多的定制和优化。

    Android应用源码之expandableList1_expandableList.zip

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

    android的ExpandableListView组件.doc

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

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

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

    Mars Android视频教程的笔记

    内容可能包括使用LocationManager和LocationListener接口,以及如何处理权限问题和优化电池使用。 5. **ExpandableListActivity.doc** - ExpandableListView是一种可扩展的列表视图,能展示层级结构的数据。这部分...

    ExpandableListView实现手风琴效果

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

    Android-ExpandableListView制作时间轴-TimeLine

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

    android安卓笔记

    - **Environment-环境**:`Environment`类提供了访问设备存储空间的方法,包括内部存储和外部存储(如SD卡)。 - **File-文件**:`File`类用于表示文件和目录路径,支持创建、删除、移动等文件操作。 #### Thread—...

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

    在这些方法中,你需要根据数据源返回相应的值,并为`groups.xml`和`childs.xml`中的UI元素填充数据。 此外,你还可以通过`setOnGroupClickListener`和`setOnChildClickListener`来监听用户对组和子项的点击事件,...

    分享Android中ExpandableListView控件使用教程

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

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

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

    android学习实例

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

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

    最后,你需要在适配器中提供`getChildView()`和`getGroupView()`方法,分别用于渲染二级列表项和一级列表项。这两个方法会根据你之前创建的布局文件(`groups.xml`和`childs.xml`)来填充数据。 ```java @Override ...

Global site tag (gtag.js) - Google Analytics