Android开发之ExpandableListActivity

什么是ExpandableListActivity?

ExpandableListActivity是Activity类的子类,是一种具有多级条目显示功能的Activity。


如何使用ExpandableListActivity?

1.让MainActivity类继承ExpandableListActivity类。

2.在activity_main布局文件中定义ExpandableListView标签。

3.创建一个布局文件,用来定义一级条目的显示格式。

4.创建一个布局文件,用来定义二级条目的显示格式。

5.创建多个List对象,分别为一级条目和二级条目提供数据。

6.创建SimpleExpandableListAdapter对象,并设置给当前的ExpandableListActivity。


activity_main.java:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.expandablelistactivity.MainActivity">

    <ExpandableListView
        android:id="@id/android:list"
        android:drawSelectorOnTop="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ExpandableListView>


</RelativeLayout>


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:textSize="25sp"
        android:paddingLeft="30sp"
        android:text="No data"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</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:text="No data"
        android:paddingLeft="30sp"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity.java:

package com.mycompany.expandablelistactivity;

import android.app.ExpandableListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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", "河南");
        Map<String, String> group2 = new HashMap<String, String>();
        group2.put("group", "山东");
        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", "郑州");
        Map<String, String> child1Data2 = new HashMap<String, String>();
        child1Data2.put("child", "洛阳");
        Map<String, String> child1Data3 = new HashMap<String, String>();
        child1Data3.put("child", "南阳");
        child1.add(child1Data1);
        child1.add(child1Data2);
        child1.add(child1Data3);

        //  定义一个List,该List对象为第二个一级条目提供二级条目数据
        List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
        Map<String, String> child2Data = new HashMap<String, String>();
        child2Data.put("child", "济南");
        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.上下文对象
        //  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});

        //  将adapter设置给当前的ExpandableListActivity
        setListAdapter(adapter);
    }
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值