转自 http://blog.csdn.net/hnyzwtf/article/details/50487228

1 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ExpandableListView
android:id="@+id/expandlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="@android:color/white"
android:dividerHeight="1dp" />

</RelativeLayout>

group布局,groupitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center_vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/img_indicator"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/tv_group_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/img_indicator"
android:text="zhang san"
android:textSize="16sp"/>
<Button
android:id="@+id/btn_group_function"
android:focusable="false"
android:clickable="true"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:gravity="right"
android:background="@drawable/btn_bg_menu" />
</RelativeLayout>

</LinearLayout>

2 由于每一个child子项中的图片和标题都不一样,因此我们要新建一个Java bean类来描述每一个子项内容
新建ChildItem.java

package com.example.model;

public class ChildItem {
private String title;//子项显示的文字
private int markerImgId;//每个子项的图标

public ChildItem(String title, int markerImgId)
{
this.title = title;
this.markerImgId = markerImgId;

}

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getMarkerImgId() {
return markerImgId;
}
public void setMarkerImgId(int markerImgId) {
this.markerImgId = markerImgId;
}

}

3 如果要将自定义的数据在ExpandableListView上显示出来,我们必须定义一个适配器

package com.example.expandablelistdemo;

import java.util.List;
import java.util.Map;

import com.example.model.ChildItem;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
* ExpandListView的适配器,继承自BaseExpandableListAdapter
*
*/
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter implements OnClickListener {

private Context mContext;
private List<String> groupTitle;
//子项是一个map,key是group的id,每一个group对应一个ChildItem的list
private Map<Integer, List<ChildItem>> childMap;
private Button groupButton;//group上的按钮

public MyBaseExpandableListAdapter(Context context, List<String> groupTitle, Map<Integer, List<ChildItem>> childMap) {
this.mContext = context;
this.groupTitle = groupTitle;
this.childMap = childMap;
}
/*
* Gets the data associated with the given child within the given group
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
//我们这里返回一下每个item的名称,以便单击item时显示
return childMap.get(groupPosition).get(childPosition).getTitle();
}
/*
* 取得给定分组中给定子视图的ID. 该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID)
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/*
* Gets a View that displays the data for the given child within the given group
*/
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
ChildHolder childHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.childitem, null);
childHolder = new ChildHolder();
childHolder.childImg = (ImageView) convertView.findViewById(R.id.img_child);
childHolder.childText = (TextView) convertView.findViewById(R.id.tv_child_text);
convertView.setTag(childHolder);
}else {
childHolder = (ChildHolder) convertView.getTag();
}
childHolder.childImg.setBackgroundResource(childMap.get(groupPosition).get(childPosition).getMarkerImgId());
childHolder.childText.setText(childMap.get(groupPosition).get(childPosition).getTitle());

return convertView;
}

/*
* 取得指定分组的子元素数
*/
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childMap.get(groupPosition).size();
}

/**
* 取得与给定分组关联的数据
*/
@Override
public Object getGroup(int groupPosition) {
return groupTitle.get(groupPosition);
}

/**
* 取得分组数
*/
@Override
public int getGroupCount() {
return groupTitle.size();
}

/**
* 取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID)
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/*
*Gets a View that displays the given group
*return: the View corresponding to the group at the specified position
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder groupHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.groupitem, null);
groupHolder = new GroupHolder();
groupHolder.groupImg = (ImageView) convertView.findViewById(R.id.img_indicator);
groupHolder.groupText = (TextView) convertView.findViewById(R.id.tv_group_text);
convertView.setTag(groupHolder);
}else {
groupHolder = (GroupHolder) convertView.getTag();
}
if (isExpanded) {
groupHolder.groupImg.setBackgroundResource(R.drawable.downarrow);
}else {
groupHolder.groupImg.setBackgroundResource(R.drawable.rightarrow);
}
groupHolder.groupText.setText(groupTitle.get(groupPosition));

groupButton = (Button) convertView.findViewById(R.id.btn_group_function);
groupButton.setOnClickListener(this);
return convertView;
}

@Override
public boolean hasStableIds() {
// Indicates whether the child and group IDs are stable across changes to the underlying data
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// Whether the child at the specified position is selectable
return true;
}
/**
* show the text on the child and group item
*/
private class GroupHolder
{
ImageView groupImg;
TextView groupText;
}
private class ChildHolder
{
ImageView childImg;
TextView childText;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_group_function:
Log.d("MyBaseExpandableListAdapter", "你点击了group button");
default:
break;
}

}
}

4 MainActivity.java

package com.example.expandablelistdemo;

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

import com.example.model.ChildItem;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;

public class MainActivity extends Activity {
private ExpandableListView expandList;
private List<String> groupData;//group的数据源
private Map<Integer, List<ChildItem>> childData;//child的数据源
private MyBaseExpandableListAdapter myAdapter;

final int CONTEXT_MENU_GROUP_DELETE = 0;//添加上下文菜单时每一个菜单项的item ID
final int CONTEXT_MENU_GROUP_RENAME = 1;
final int CONTEXT_MENU_CHILD_EDIT = 2;
final int CONTEXT_MENU_CHILD_DELETE = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
initView();
initEvents();
}
/**
* group和child子项的数据源
*/
private void initDatas() {

groupData = new ArrayList<String>();
groupData.add("红色水果");
groupData.add("黄色水果");
groupData.add("其他水果");

List<ChildItem> childItems = new ArrayList<ChildItem>();
ChildItem childData1 = new ChildItem("苹果", R.drawable.apple_pic);
childItems.add(childData1);
ChildItem childData2 = new ChildItem("樱桃", R.drawable.cherry_pic);
childItems.add(childData2);
ChildItem childData3 = new ChildItem("草莓", R.drawable.strawberry_pic);
childItems.add(childData3);

List<ChildItem> childItems2 = new ArrayList<ChildItem>();
ChildItem childData4 = new ChildItem("香蕉", R.drawable.banana_pic);
childItems2.add(childData4);
ChildItem childData5 = new ChildItem("芒果", R.drawable.mango_pic);
childItems2.add(childData5);
ChildItem childData6 = new ChildItem("橘子", R.drawable.orange_pic);
childItems2.add(childData6);
ChildItem childData7 = new ChildItem("梨子", R.drawable.pear_pic);
childItems2.add(childData7);

List<ChildItem> childItems3 = new ArrayList<ChildItem>();
ChildItem childData8 = new ChildItem("葡萄", R.drawable.grape_pic);
childItems3.add(childData8);
ChildItem childData9 = new ChildItem("西瓜", R.drawable.watermelon_pic);
childItems3.add(childData9);

childData = new HashMap<Integer, List<ChildItem>>();
childData.put(0, childItems);
childData.put(1, childItems2);
childData.put(2, childItems3);

myAdapter = new MyBaseExpandableListAdapter(this, groupData, childData);
}

private void initView() {
expandList = (ExpandableListView) findViewById(R.id.expandlist);
//在drawable文件夹下新建了indicator.xml,下面这个语句也可以实现group伸展收缩时的indicator变化
//expandList.setGroupIndicator(this.getResources().getDrawable(R.drawable.indicator));
expandList.setGroupIndicator(null);//这里不显示系统默认的group indicator
expandList.setAdapter(myAdapter);
registerForContextMenu(expandList);//给ExpandListView添加上下文菜单
}
private void initEvents() {
//child子项的单击事件
expandList.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "你单击了:"
+myAdapter.getChild(groupPosition, childPosition), Toast.LENGTH_SHORT).show();
return true;
}
});

}
/*
* 添加上下文菜单
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {

super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo)menuInfo;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
menu.setHeaderTitle("Options");
menu.add(0, CONTEXT_MENU_GROUP_DELETE, 0, "删除");
menu.add(0, CONTEXT_MENU_GROUP_RENAME, 0, "重命名");
}
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
menu.setHeaderTitle("Options");
menu.add(1, CONTEXT_MENU_CHILD_EDIT, 0, "编辑");
menu.add(1, CONTEXT_MENU_CHILD_DELETE, 0, "删除");
}

}
/*
* 每个菜单项的具体点击事件
*/
@Override
public boolean onContextItemSelected(MenuItem item) {

ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case CONTEXT_MENU_GROUP_DELETE:
Toast.makeText(this, "这是group的删除", Toast.LENGTH_SHORT).show();
break;
case CONTEXT_MENU_GROUP_RENAME:
Toast.makeText(this, "这是group的重命名", Toast.LENGTH_SHORT).show();
break;
case CONTEXT_MENU_CHILD_EDIT:
Toast.makeText(this, "这是child的编辑", Toast.LENGTH_SHORT).show();
break;
case CONTEXT_MENU_CHILD_DELETE:
Toast.makeText(this, "这是child的删除", Toast.LENGTH_SHORT).show();
break;

default:
break;
}

return super.onContextItemSelected(item);
}
}

折叠ListView的更多相关文章

  1. 使用 ExpandableListView 实现折叠ListView

    1:layout/expandablelistview_groups.xml 标题文件 <?xml version="1.0" encoding="utf-8&qu ...

  2. listview嵌套gridview,并实现grid元素部分显示以及点击展开与折叠

    原文链接:http://blog.csdn.net/duguju/article/details/49538341 有时我们需要用GridView显示目录列表,有时甚至是二级的,即listview每一 ...

  3. 简易模仿手机拨号盘浮在ListView之上并且展开,折叠效果

    2013-12-24 16:56:45 有时候可以看到很多手机会将Call log list和Dailer放在同一个页面中,同时Dialer是可以折叠.打开的,自己做了一个Demo,能实现这种效果,简 ...

  4. cocos2dx-lua使用UIListView制作二级折叠菜单

    折叠菜单,用过jquery accordion的同学都知道是啥玩艺儿~,图片效果就是介样: cocos2dx不带有此控件,因此我们动手来实现一个. 原理很简单,展开的时候往listview里inser ...

  5. ListView Web 服务器控件概述(MSDN)

    1: "折叠"图像"展开"图像"复制"图像"复制悬停"图像 全部折叠全部展开 代码:全部 代码:多个 代码:Visual ...

  6. 【转】Android折叠效果实现案例

    源文:http://mobile.51cto.com/abased-401983.htm 为了使界面的效果更加绚丽,体验效果更佳,往往需要开发者们自行开发新的界面效果,在这里,我将奉上各种实现折叠效果 ...

  7. 09 ListView监听 ExpandableListView的使用总结

    1.ListView的滚动监听 >setOnScrollListener 监听 //ListVIew滚动监听 lv.setOnScrollListener(new OnScrollListene ...

  8. Android FoldingLayout 折叠布局 原理及实现(二)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/44283093,本文出自:[张鸿洋的博客] 1.概述 在上一篇Android Fo ...

  9. jquery.mobile 中 collapsible-set collapsible listview 共同布局问题

    最近项目用上了jquery.mobile这货,在手机上做点简单的显示.之前只知道有这个框架,没把玩过. 特别是事件绑定方面,相比桌面系统下浏览器用着各种不爽,不得要领. 如下图,在做后台系统时,一般左 ...

随机推荐

  1. Machine Learning Algorithms Study Notes(4)—无监督学习(unsupervised learning)

    1    Unsupervised Learning 1.1    k-means clustering algorithm 1.1.1    算法思想 1.1.2    k-means的不足之处 1 ...

  2. 洛谷U4807抽水机[最小生成树]

    题目背景 kkk被Farmer John和他的奶牛贝茜虐的很惨,然后她也想体验下一个Farmer的生活.但她又懒得种地,就选择养鱼. 题目描述 这些鱼都是热带鱼(废话),很娇贵(比kkk娇贵),要经常 ...

  3. guava函数式编程

    [Google Guava] 4-函数式编程 原文链接 译文链接 译者:沈义扬,校对:丁一 注意事项 截至JDK7,Java中也只能通过笨拙冗长的匿名类来达到近似函数式编程的效果.预计JDK8中会有所 ...

  4. 嵌入式linux根文件系统制作

    编译Busybox 从http://www.busybox.net/downloads/下载busybox工具.这里我们下载的上最新版: busybox-1.24.2.tar.bz2 解压Busybo ...

  5. [No000005]C#注册表操作,创建,删除,修改,判断节点是否存在

    //用.NET下托管语言C#操作注册表,主要内容包括:注册表项的创建,打开与删除.键值的创建(设置值.修改),读取和删除.判断注册表项是否存在.判断键值是否存在. //准备工作: //1:要操作注册表 ...

  6. java 27 - 2 反射之 反射的概述以及获取Class文件对象的方式

    反射: JAVA语言的反射机制: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它的任意一个方法和属性: 这种动态获取的信息以及动态调 ...

  7. Eclipse块选取的情况 shift+tab 是块向前缩进

    Eclipse块选取的情况 shift+tab 是块向前缩进

  8. 转: VMware 安装mac osx 10.11 安装步骤(一)(from伟东)

    http://blog.csdn.net/soachenshui/article/details/49251513

  9. PAT 1005. 继续(3n+1)猜想 (25) JAVA

    当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候,我们需要计算3.5.8.4.2.1,则当我们对n=5.8.4.2进行验证的时候,就可以直接 ...

  10. Java核心技术点之动态代理

    本篇博文会从代理的概念出发,介绍Java中动态代理技术的使用,并进一步探索它的实现原理.由于个人水平有限,叙述中难免出现不清晰或是不准确的地方,希望大家可以指正,谢谢大家:) 一.概述 1. 什么是代 ...