北大青鸟

首页 > 安卓技术教程

站内公告

解析Android控件之ExpandableListView

责任编辑:宏鹏来源:武汉北大青鸟鲁广校区时间:10-17
导读:Android控件之ExpandableListView学习。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="//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:id="@+id/group"

android:textSize="25sp"

android:paddingLeft="35px"

android:paddingTop="10px"

android:paddingRight="5px"

android:paddingBottom="10px"

android:text="No Data"/>

</LinearLayout>

  childs.xml:

  <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="//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:id="@+id/child"

android:textSize="15sp"

android:paddingLeft="25px"

android:paddingTop="10px"

android:paddingRight="5px"

android:paddingBottom="10px"

android:text="No Data"/>

</LinearLayout>

  先,个布局文件main.xml是在主界面定义一个ExpandableListView ,在这里我们就要和在做ListView是的方法做下对比了,其实这个listView的显示类似。主要是用来显示ExpandableListView 下的数据。第二个布局文件是定义ExpandableListView 下的一级条目目录。在这里我只为一级条目目录添加一个textView控件。第三个布局文件是定义ExpandableListView 下的二级条目目录,和一级条目目录一样,布局文件里也只有一个TextView控件。

  Java代码:

  ExpandableActivity:

package cn.yj3g.ExpandableListActivity;

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.view.View;

import android.widget.ExpandableListView;

import android.widget.SimpleExpandableListAdapter;

public class ExpandableActivity 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<Map<String, String>> child1 = new ArrayList<Map<String, String>>();

//同样是在一级条目目录下创建两个对应的二级条目目录

Map<String, String> childdata1 = new HashMap<String, String>();

childdata1.put("child", "青花瓷");

Map<String, String> childdata2 = new HashMap<String, String>();

childdata2.put("child", "东风破");

child1.add(childdata1);

child1.add(childdata2);

//同上

List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();

Map<String, String> childdata3 = new HashMap<String, String>();

childdata3.put("child", "青花瓷.lrc");

Map<String, String> childdata4 = new HashMap<String, String>();

childdata4.put("child", "东风破.lrc");

child2.add(childdata3);

child2.add(childdata4);

// 将二级条目放在一个集合里,供显示时使用

List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

childs.add(child1);

childs.add(child2);

SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(

this, groups, R.layout.groups, new String[] { "group" },

new int[] { R.id.group }, childs, R.layout.child,

new String[] { "child" }, new int[] { R.id.child });

setListAdapter(adapter);

}

@Override

public boolean setSelectedChild(int groupPosition, int childPosition,

boolean shouldExpandGroup) {

//do something

return super.setSelectedChild(groupPosition, childPosition,

shouldExpandGroup);

}

@Override

public void setSelectedGroup(int groupPosition) {

//do something

super.setSelectedGroup(groupPosition);

}

@Override

public boolean onChildClick(ExpandableListView parent, View v,

int groupPosition, int childPosition, long id) {

//do something

return super.onChildClick(parent, v, groupPosition, childPosition, id);

}

}

上面在显示ExpandableListView 是用的是SimpleExpandableListAdapter ,这里要传的参数比较多,大家在用时别一看到参数多就头疼,没事的,看注释你就知道各个参数的意思了。下面的三个重写方法是在显示 ExpandableListView 是调用的,具体的用法就要根据需求来确定了。

    看过该安卓技术教程的还看过

本文标题:解析Android控件之ExpandableListView,责任编辑:宏鹏,来源:武汉北大青鸟鲁广校区栏目,于10-17发布于北大青鸟鲁广校区。Android控件之ExpandableListView学习。

专业老师指导

赵老师

赵老师

从事IT教育培训十年有余,致力于帮助广大学子找到适合自己的专业

立即在线咨询

培训咨询客服

陈老师

陈老师

IT培训专业客服,用自己的真诚解决了无数学子的困惑

立即在线咨询

本文地址:https://m.027hpedu.com/html/1777.html

文章标题:解析Android控件之ExpandableListView

上一篇:

下一篇:

热点关注

推荐安卓技术教程

热门安卓技术教程

初中生
高中生
大学生
打工者
男生
女生

预约你的精彩未来

预约将免费领取7天课程体验卡

-------请选择试预约课程-------

JAVA
WEB前端
PHP
UI设计
Python
电子商务
视频剪辑
大数据工程师
平面设计

83345人已领取

全国百余家校区

只为您方便就学

北大青鸟鲁广校区

北大青鸟鲁广校区

武汉市洪山区珞喻路724号(地铁二号线光谷广场站F口出)

预约到校
领取学习大礼包

首页

热门课程

视频网课

新闻资讯

关于学校

联系学校

预约选课申请

  • 预约时间

    请选择预约时间

  • 预约课程

    请选择预约课程

  • 姓   名
  • 手机号
  • QQ 号
  • 微信号

添加老师微信号

专业老师24小时1对1学习指导

定制专属于你的专属学习方案

微信号:17740513250

复制老师的微信号

复制成功啦

快去微信添加老师为好友吧~

北大青鸟小青

微信号:17740513250

北大青鸟小青

微信号:17740513250

设置备注
小主知道啦