layout.xml

<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=".MainActivity" > <ExpandableListView
android:id="@+id/expandable_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView> </RelativeLayout>

expandable_childe_item.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/child_text"
android:layout_height="wrap_content"
android:layout_width="match_parent"/> </LinearLayout>

expandable_group_item.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/group_text"
android:layout_height="wrap_content"
android:layout_width="match_parent"/> </LinearLayout>

main.java

package com.example.day09_expandablelistview;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView; public class MainActivity extends Activity {
private int lastGroupPosition = -1;
private ExpandableListView expandable_lv;
private String groupData[] = {"同事","老师","朋友"};
private String childData[][] = {{"小小","小明","饭饭","流浪"},{"李老师","张老师","吴老师","肖老师","柳老师"},{"雯雯","哔哔","嘻嘻"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandable_lv = (ExpandableListView) findViewById(R.id.expandable_lv);
//给ExpandableListAdapter设置适配器---自定义适配器需要继承BaseExpandableListAdapter()实现其中的方法
MyExpandableListAdapter myExpandableListAdapter = new MyExpandableListAdapter();
//设置适配器
expandable_lv.setAdapter(myExpandableListAdapter);
//去掉group默认的箭头
expandable_lv.setGroupIndicator(null);
//设置组可拉伸的监听器,拉伸时会调用其中的onGroupExpand()方法
expandable_lv.setOnGroupExpandListener(new OnGroupExpandListener() { @Override
public void onGroupExpand(int groupPosition) {
/**
* 实现打开只能打开一个组的功能,打开一个组,已将打开的组会自动收缩
*/
if(lastGroupPosition != groupPosition){
expandable_lv.collapseGroup(lastGroupPosition);
}
lastGroupPosition = groupPosition;
}
});
//设置组收缩的监听器,收缩时会调用其中的onGroupCollapse()方法
expandable_lv.setOnGroupCollapseListener(new OnGroupCollapseListener() { @Override
public void onGroupCollapse(int groupPosition) { }
});
}
/**
* 自定义实现类继承BaseExpandandableListAdapter
* @author my
*
*/
class MyExpandableListAdapter extends BaseExpandableListAdapter{
/**
* 得到组的数量
*/
@Override
public int getGroupCount() {
return groupData.length;
}
/**
* 得到每个组的元素的数量
*/
@Override
public int getChildrenCount(int groupPosition) {
return childData[groupPosition].length;
}
/**
* 获得组的对象
*/
@Override
public Object getGroup(int groupPosition) {
return groupData[groupPosition];
}
/**
* 获得子对象
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return childData[groupPosition][childPosition];
}
/**
* 得到组id
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* 得到子id
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 表示数据是否稳定,对监听事件有影响
*/
@Override
public boolean hasStableIds() {
return true;
}
/**
* 确定一个组的展示视图--groupPosition表示的当前需要展示的组的索引
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater systemService = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = systemService.inflate(R.layout.enpandable_group_item, null);
TextView group_text = (TextView) view.findViewById(R.id.group_text);
group_text.setText(groupData[groupPosition]);
return view;
}
/**
* 确定一个组的一个子的展示视图--groupPostion表示当前组的索引,childPosition表示的是需要展示的子的索引
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater systemService = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = systemService.inflate(R.layout.enpandable_child_item, null);
TextView child_text = (TextView) view.findViewById(R.id.child_text);
child_text.setText(childData[groupPosition][childPosition]);
return view;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
} } }

可扩展的listview--Expandablelistview的更多相关文章

  1. 027 Android 可扩展的listview:ExpandableListView的使用案例

    1.ExpandableListView简介 ExpandableListView是一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选 ...

  2. 分组的listview——ExpandableListView

    开发使用到的数据统计时可以用分组的ExpandablelistView 效果:

  3. Yii2 分页类的扩展和listview引用

    Yii2 本身提供了不错分页选项供用户设置,但是实际项目中我们往往需要复杂一些的分页样式,例如下图所示的效果,上下翻页可用和不可用均用图标来替换.

  4. QQ分组显示列表ExpandableListView组件应用源码

    ExpandableListView又称为可扩展的ListView组件,他和ListView组件很相似 不过每行的显示有两个xml文件,一个xml文件用于定义分组列表的显示风格, 还有一个xml文件用 ...

  5. 解决android expandablelistview 里面嵌入gridview行数据重复问题

    最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字. 很简单 ...

  6. 利用ExpandableListView和gridview 显示可展开折叠菜单导航

    这篇随身笔带来的是结合聚合数据“菜谱大全”做的一个菜谱可折叠一级+二级列表. 先发来一些截图一睹为快吧. ExpandableListView 可用于折叠型菜单列表,其布局主要通过getGroupVi ...

  7. ExpandableListView使用

    相关博客 ExpandableListView使用 博客内容记录 场景 有时候,使用ListView并不能满足应用程序所需要的功能.有些应用程序需要多组ListView,这时候我们就要使用一种新的控件 ...

  8. Android数据适配-ExpandableListView

    Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableList ...

  9. Android ExpandableListView的使用详解

    ExpandableListView(可扩展的ListView) ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组 ...

  10. ListView上拉加载,下拉刷新 PullToRefresh的使用

    PullToRefresh是一套实现非常好的下拉刷新库,它支持:ListViewExpandableListViewGridViewWebViewScrollViewHorizontalScrollV ...

随机推荐

  1. delphi获得当前鼠标坐标

    简单的说就是取鼠标所在位置对应的窗口句柄? procedure TForm1.Timer1Timer(Sender: TObject);vara:TPoint; //用来存放坐标hw:HWND; // ...

  2. opencv 在工业中的应用:圆孔定位

    在工业中产品或者夹具上经常有圆形孔,我们可以利用这些孔来对产品或者夹具进行定位.我用OPENCV写了个DEMO 程序,介绍如下: (1)首先点击打开图像按钮打开一幅图像 (2)进行一些参数设置 (3) ...

  3. NOIP2003 传染病控制

    题四     传染病控制 [问题背景] 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国 大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完 全认 ...

  4. 【转载】sed命令详解

    [转载自]http://www.cnblogs.com/edwardlost/archive/2010/09/17/1829145.html   sed -i  把后面的操作后的文本输出回原文本   ...

  5. HDU-4605 Magic Ball Game 树状数组+离散+dfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4605 题意:给一颗树,每个节点有个权值w[u],每个节点只有两个儿子或者没有儿子,从根节点放下一个小球 ...

  6. Android应用正确使用扩展SD卡,特别是安卓4.4以后的版本

    Android 开发时如何正确获取使用扩展存储路径 在 2.x 版本中,Android设备都是单存储,第三方App写文件,必须申请 WRITE_EXTERNAL_STORAGE 权限: 在4.0之后, ...

  7. Win7旗舰版的nfs服务器如何架设? - Microsoft Community

    Win7旗舰版的nfs服务器如何架设? - Microsoft Community Win7旗舰版的nfs服务器如何架设?

  8. [iOS基础控件 - 6.7] 微博展示 使用代码自定义TableCell(动态尺寸)

    A.需求 1.类似于微博内容的展示 2.头像 3.名字 4.会员标志 5.内容 6.分割线 7.配图(可选,可有可无)   code source: https://github.com/hellov ...

  9. iOS开发—字典转模型,KVC设计模式

    iOS开发UI基础—字典转模型 开发中,通常使用第三方框架可以很快的实现通过字典转模型,通过plist创建模型,将字典的键值对转成模型属性,将模型转成字典,通过模型数组来创建一个字典数组,通过字典数组 ...

  10. 转载总结 C# 多态(虚方法,抽象,接口实现)

    前言:我们都知道面向对象的三大特性:封装,继承,多态.封装和继承对于初学者而言比较好理解,但要理解多态,尤其是深入理解,初学者往往存在有很多困惑,为什么这样就可以?有时候感觉很不可思议,由此,面向对象 ...