android ExpandableListView
本文采用一个Demo来展示Android中ExpandableListView控件的使用,如如何在组/子ListView中绑定数据源。直接上代码如下:
程序结构图:
layout目录下的 main.xml 文件源码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <!-- 我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式
- 如果自定义listview的显示方式这里这个android:id="@id/android:list" 必须这样写 -->
- <!-- android:drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会
- 挡住(覆盖)内容 , 如果这是为false就表示不会覆盖掉 -->
- <ExpandableListView
- android:id="@id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"/>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式 如果自定义listview的显示方式这里这个android:id="@id/android:list" 必须这样写 --><!-- android:drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会挡住(覆盖)内容 , 如果这是为false就表示不会覆盖掉 --> <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawSelectorOnTop="false"/>
</LinearLayout>
包 com.andyidea.demo中ContactsActivity.java源码如下:
- package com.andyidea.demo;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.ExpandableListActivity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.widget.AbsListView;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.TextView;
- public class ContactsActivity extends ExpandableListActivity {
- List<String> group; //组列表
- List<List<String>> child; //子列表
- ContactsInfoAdapter adapter; //数据适配器
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE); //设置为无标题
- setContentView(R.layout.main);
- getExpandableListView().setBackgroundResource(R.drawable.default_bg);
- initializeData();
- getExpandableListView().setAdapter(new ContactsInfoAdapter());
- getExpandableListView().setCacheColorHint(0); //设置拖动列表的时候防止出现黑色背景
- }
- /**
- * 初始化组、子列表数据
- */
- private void initializeData(){
- group = new ArrayList<String>();
- child = new ArrayList<List<String>>();
- addInfo("Andy",new String[]{"male","138123***","GuangZhou"});
- addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});
- addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});
- addInfo("Tom",new String[]{"female","138123***","ShangHai"});
- addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});
- }
- /**
- * 模拟给组、子列表添加数据
- * @param g-group
- * @param c-child
- */
- private void addInfo(String g,String[] c){
- group.add(g);
- List<String> childitem = new ArrayList<String>();
- for(int i=0;i<c.length;i++){
- childitem.add(c[i]);
- }
- child.add(childitem);
- }
- class ContactsInfoAdapter extends BaseExpandableListAdapter{
- //-----------------Child----------------//
- @Override
- public Object getChild(int groupPosition, int childPosition) {
- return child.get(groupPosition).get(childPosition);
- }
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
- @Override
- public int getChildrenCount(int groupPosition) {
- return child.get(groupPosition).size();
- }
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- String string = child.get(groupPosition).get(childPosition);
- return getGenericView(string);
- }
- //----------------Group----------------//
- @Override
- public Object getGroup(int groupPosition) {
- return group.get(groupPosition);
- }
- @Override
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
- @Override
- public int getGroupCount() {
- return group.size();
- }
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- String string = group.get(groupPosition);
- return getGenericView(string);
- }
- //创建组/子视图
- public TextView getGenericView(String s) {
- // Layout parameters for the ExpandableListView
- AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, 40);
- TextView text = new TextView(ContactsActivity.this);
- text.setLayoutParams(lp);
- // Center the text vertically
- text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
- // Set the text starting position
- text.setPadding(36, 0, 0, 0);
- text.setText(s);
- return text;
- }
- @Override
- public boolean hasStableIds() {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return true;
- }
- }
- }
package com.andyidea.demo;import java.util.ArrayList;
import java.util.List;import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;public class ContactsActivity extends ExpandableListActivity {List<String> group; //组列表List<List<String>> child; //子列表ContactsInfoAdapter adapter; //数据适配器/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE); //设置为无标题setContentView(R.layout.main);getExpandableListView().setBackgroundResource(R.drawable.default_bg);initializeData();getExpandableListView().setAdapter(new ContactsInfoAdapter());getExpandableListView().setCacheColorHint(0); //设置拖动列表的时候防止出现黑色背景}/*** 初始化组、子列表数据*/private void initializeData(){group = new ArrayList<String>();child = new ArrayList<List<String>>();addInfo("Andy",new String[]{"male","138123***","GuangZhou"});addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});addInfo("Tom",new String[]{"female","138123***","ShangHai"});addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});}/*** 模拟给组、子列表添加数据* @param g-group* @param c-child*/private void addInfo(String g,String[] c){group.add(g);List<String> childitem = new ArrayList<String>();for(int i=0;i<c.length;i++){childitem.add(c[i]);}child.add(childitem);}class ContactsInfoAdapter extends BaseExpandableListAdapter{//-----------------Child----------------//@Overridepublic Object getChild(int groupPosition, int childPosition) {return child.get(groupPosition).get(childPosition);}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic int getChildrenCount(int groupPosition) {return child.get(groupPosition).size();}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {String string = child.get(groupPosition).get(childPosition); return getGenericView(string);}//----------------Group----------------//@Overridepublic Object getGroup(int groupPosition) {return group.get(groupPosition);} @Overridepublic long getGroupId(int groupPosition) {return groupPosition;} @Overridepublic int getGroupCount() {return group.size();} @Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {String string = group.get(groupPosition); return getGenericView(string);}//创建组/子视图 public TextView getGenericView(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 40);TextView text = new TextView(ContactsActivity.this); text.setLayoutParams(lp); // Center the text vertically text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position text.setPadding(36, 0, 0, 0); text.setText(s); return text; } @Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;} @Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}}
}
最后,程序运行后截图如下:
转载于:https://www.cnblogs.com/lzjsky/p/4938956.html
android ExpandableListView相关推荐
- android expandablelistview横向,Android ExpandableListView使用小结(二)
在上一篇Android ExpandableListView使用小结(一)的介绍中,我们学习了 ExpandableListView 的使用方法,学习了自定义适配器,为列表选项设置监听事件.想必大家都 ...
- Android ExpandableListView示例教程
Welcome to Android ExpandableListView Example Tutorial. In this tutorial we'll implement an Expandab ...
- NavigationView内的Android ExpandableListView
In this tutorial, we'll be implementing an application in which we display an ExpandableListView of ...
- android expandablelistview横向,Android ExpandableListView使用小结(一)
ExpandableListView 是什么? 官方给出的解释是: A view that shows items in a vertically scrolling two-level list. ...
- Android ExpandableListView几个特殊的属性
原帖地址:http://blog.csdn.net/t12x3456/article/details/7828620 1. 设置ExpandableListView 默认是展开的: 先实例化 exLi ...
- list多选 android,Android ExpandableListView单选以及多选实现代码
一.概述 ExpandableListView是常用的一个控件,今天自己做了个小练习,主要需求是单选以及多选的实现,看似比较简单,但是还是比较复杂,把代码贴给大家,有这种需求的可以参考一下. 二.效果 ...
- Android ExpandableListView 展开列表控件(手机QQ好友列表)
你是否觉得手机QQ上的好友列表那个控件非常棒? 不是..... 那也没关系, 学多一点知识对自己也有益无害. 那么我们就开始吧. 展开型列表控件, 原名ExpandableListView 是普 ...
- Android ExpandableListView 使用实例
API中类继承关系: ExpandableListView extends ListView java.lang.Object ↳ android.view.View ↳ androi ...
- android可扩展列表,android ExpandableListView可扩展列表
http://leiwuluan.iteye.com/blog/1508356 先看一效果图. 列表中要有 图片和文字: 所以我们要实现一个自定义的 适配器. 介绍一个类:BaseExpandab ...
最新文章
- 算法-电话号码的字母组合
- CentOS6.5安装MySQL5.1.73
- 信号与系统奥本海姆pdf_2019上海交通大学819信号系统与信号处理考研初/复试经验...
- (软件工程复习核心重点)第六章实现和测试-第二节:软件测试基础
- IntelliJ Idea 常用12款插件(提高开发效率),附优秀主题插件
- spring boot部署实战
- Material Dialogs
- [jQuery]20+ Brilliant and Advanced jQuery Effects
- SpringBoot+Vue项目校园运动会管理系统
- PayPal的钱如何提现?PayPal商家终于可以松口气了!
- flask中的jinjia2模板引擎详解1
- 关于使用itextpdf生成pdf的页面横向和竖向
- 1、目标检测 RCNN(翻译+标注)
- 可调光的LED驱动解决方案
- MAC--终端Terminal 快捷键
- gh风电服务器系统 使用,基于GH Bladed风电机组故障模拟与诊断研究
- ubuntu系统终端使用命令删除文件到回收站中
- ICSE 2019 会议论文总结
- Ubuntu18 查看CUDA的CUDA_ARCH_BIN
- CPU卡程序设计实例(十)ESAM复位程序
热门文章
- shiro-cas------实现单点登出并自定义登出starter
- windows 配置 Gitlab、Gitee(码云) 的git开发环境
- linux安装jdk8和tomcat8
- 数据科学家教你用数据模型来泡妞
- 鸿蒙系统可以安装teams吗,鸿蒙致命弱点被曝光!不能装这个软件,80%用户将望而却步!...
- php程序阻塞与非阻塞的区别,完美起航
- 社区发现(六)--模块度
- 蓝牙学习笔记(十)——BLE透传传输延迟分析
- 电路——I/O口定时翻转电平驱动蜂鸣器注意事项
- Mysql binlog入门