可扩展的listview--Expandablelistview
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的更多相关文章
- 027 Android 可扩展的listview:ExpandableListView的使用案例
1.ExpandableListView简介 ExpandableListView是一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选 ...
- 分组的listview——ExpandableListView
开发使用到的数据统计时可以用分组的ExpandablelistView 效果:
- Yii2 分页类的扩展和listview引用
Yii2 本身提供了不错分页选项供用户设置,但是实际项目中我们往往需要复杂一些的分页样式,例如下图所示的效果,上下翻页可用和不可用均用图标来替换.
- QQ分组显示列表ExpandableListView组件应用源码
ExpandableListView又称为可扩展的ListView组件,他和ListView组件很相似 不过每行的显示有两个xml文件,一个xml文件用于定义分组列表的显示风格, 还有一个xml文件用 ...
- 解决android expandablelistview 里面嵌入gridview行数据重复问题
最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字. 很简单 ...
- 利用ExpandableListView和gridview 显示可展开折叠菜单导航
这篇随身笔带来的是结合聚合数据“菜谱大全”做的一个菜谱可折叠一级+二级列表. 先发来一些截图一睹为快吧. ExpandableListView 可用于折叠型菜单列表,其布局主要通过getGroupVi ...
- ExpandableListView使用
相关博客 ExpandableListView使用 博客内容记录 场景 有时候,使用ListView并不能满足应用程序所需要的功能.有些应用程序需要多组ListView,这时候我们就要使用一种新的控件 ...
- Android数据适配-ExpandableListView
Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableList ...
- Android ExpandableListView的使用详解
ExpandableListView(可扩展的ListView) ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组 ...
- ListView上拉加载,下拉刷新 PullToRefresh的使用
PullToRefresh是一套实现非常好的下拉刷新库,它支持:ListViewExpandableListViewGridViewWebViewScrollViewHorizontalScrollV ...
随机推荐
- 55人班37人进清华北大的金牌教师之32条教育建言! z
他带的一个55人的班,37人考进清华.北大,10人进入剑桥大学.耶鲁大学.牛津大学等世界名校并获全额奖学金,其他考入复旦.南开等大学.不仅 如此,校足球冠军.校运动会总冠军.校网页设计大赛总冠军等6项 ...
- Linux shell命令
一.删除监听指定端口的进程: lsof -ti: 80 | xargs kill -9 -t: 输出pid -i:查看指定端口占用情况 二.查看可执行文件动态链接库相关信息 ldd <可执行文件 ...
- IO_REMOVE_LOCK使用方法小结(转载加改正)
原文链接:http://www.programlife.net/io_remove_lock.html IO_REMOVE_LOCK(删除锁)的具体结构没有公开,WDK的文档中中查不到IO_REMOV ...
- 让浏览器进行跨域访问, 开发阶段需要跨域访问的测试方案 chrome的快捷方式里面 加 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --args --disable-web-security
Chrome浏览器 的快捷方式里加一个 命令可以使浏览器进行跨域访问 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe ...
- Java实现在访问者模式中使用反射
集合类型在面向对象编程中很常用,这也带来一些代码相关的问题.比如,“怎么操作集合中不同类型的对象?” 一种做法就是遍历集合中的每个元素,然后根据它的类型而做具体的操作.这会很复杂,尤其当你不知道集合中 ...
- Excel里的单元格提行
最近老板发了个表,遇到了个小知识点,收藏以后有用!拿来学学 如下,是属于单元格.怎么提行? 直接,空格或space,都无法解决!...... 解决办法:Alt + Enter 成功! 参考: http ...
- python 错误、调试和测试
在程序运行过程中,总会遇到各种各样的错误. 有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,这种错误我们通常称之为bug,bug是必须修复的. 有的错误是用户输入造成的,比如让用 ...
- C/C++流程图生成器 C转流程图【worldsing笔记】
此版本仅供学习,请大家支持正版软件!! AutoFlowChart v3.1软件下载: http://url.cn/OUK17C 支持导出:word.visio.图片格式. 例如:main.c # ...
- 解决eclipse 使用run运行,始终会跳到debug模式!
查看此选项是否选择中卫always,若是,更改为never或prompt,重启eclipse即可
- MSSQLSERVER数据库- 一条代码搞定单表备份表结构和表数据
在百度上找到的,很实用这个容易操作,不就两张表,我的建议就是备份表带上日期,以便以后要恢复数据的时候,可以快速找到他,这样备份是表结构和数据一起处理.select * into share_20090 ...