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. Java多线程 -- 深入理解JMM(Java内存模型) --(五)锁

    锁的释放-获取建立的happens before 关系 锁是Java并发编程中最重要的同步机制.锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息. 下面是锁释放-获取的示例代 ...

  2. 【转】Linux(Ubuntu)下面SecureCRT 完全破解

    仅供测试, 勿用作商业用途.首先要到vandyke网站下载一个securecrt, 需要注册.http://www.vandyke.com/download/securecrt/download.ht ...

  3. NGINX(一)内存结构

    ngx_buf_t和ngx_chain_t是nginx中操作内存的重要手段, 很多的数据都需要通过这个结构进行保存. 其中ngx_buf_t中保存一块可用内存, ngx_chain_t则是将内存块连接 ...

  4. chrome启用本地文件

    chrome禁止本地浏览时加载本地其他文件,可以采用添加启动参数的方式来支持 添加参数为 --allow-file-access-from-files  或者 --disable-web-securi ...

  5. App是什么,可以分为几类?及其相关解释。

     App,是应用程序,Application的缩写,事实上,严格说来,目前市面上的APP大致可分为以下十类,即移动UGC,移动搜索,移动浏览,移动支付,移动广告,移动即时信息,SNS,LBS,AR以及 ...

  6. LeetCode题解——Median of Two Sorted Arrays

    题目: 找两个排序数组A[m]和B[n]的中位数,时间复杂度为O(log(m+n)). 解法: 更泛化的,可以找第k个数,然后返回k=(m+n)/2时的值. 代码: class Solution { ...

  7. VBScript: 正则表达式(RegExp对象)

    RegExp对象是VBScript中用于提供简单地正则表达式支持的对象.VBScript中所有和正则表达式有关的属性和方法都有这个对象有关联. 一.RegExp对象的属性和方法(三个属性,三个方法) ...

  8. activemq 的小实验

    package ch02.chat; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms ...

  9. wuzhicms 无规律推荐位标签的嵌套使用

    这种布局的页面,要做推荐位的话,往往需要做3个推荐位.但这样就得维护3个推荐位内容. 其实可以通过简单粗暴的方式 一个推荐位即可搞定. 如下代码:  {wz:content action=" ...

  10. 深入浅出 JavaScript 对象 v0.5

    JavaScript 没有类的概念,因此它的对象与基于类的语言中的对象有所不同.笔者主要参考<JS 高级程序设计>.<JS 权威指南>和<JS 精粹> 本文由浅入深 ...