【Android基础篇】使用ExpandableListView实现自定义的下拉列表

1. ExpandableListView简介

下拉列表(可扩展的列表控件)在App应用非常常见,在Android开发中是我们必须掌握的一个控件,下面就来介绍一下ExpandableListView这个控件的开发。

ExpandableListView分为组列表项和子列表项,单击组列表项,会显示这组里所有的子列表项。和ListView一样,它也是通过Adapter数据适配器完成数据与显示的衔接,但它使用的另一种接口:ExpandableListAdapter.

今天我们要做的是实现一个继承它的父接口:BaseExpandableListAdapter的类,来实现自定义的下拉列表(就是如同ListView里的方式,给它的列表项设置界面布局文件layout)。

2. 示例

示例包含四个文件,两个Java文件和两个layout文件。Java文件包含一个Activity,和我们自己实现的MyExpandAdapter;layout文件一个是用于Activity,另一个就是子列表项的界面布局文件,至于组列表项的布局,我们通过直接在代码里创建一个TextView来实现,这样,就同时介绍了下拉列表样式实现的两种方法。

plan_manager.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/expandableListView_plan_manager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</LinearLayout>

布局文件,添加控件

PlanManager.java

package com.plan;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.example.plan.R;

public class PlanManager extends Activity{
   

    ExpandableListView sp_date_list = null;     //列表
    MyExpandAdapter adapter = null;             //数据适配器

    List<String> group_head;                    //组列表项,每个组都有一个子List
    List<List<String>> child;                   
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值