Android中ExpandableListView的使用
Android中ExpandableListView的使用
ExpandableListView是Android中可以实现下拉list的一个控件,具体的实现方法如下:
首先:在layout的xml文件中定义一个ExpandableListView
- <LinearLayout
- android:id="@+id/linearLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- androidrientation="vertical"
- >
- <ExpandableListView
- android:id="@+id/expandableListView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
定义两个List,用来存放控件中Group/Child中的String
- private List<String> groupArray;
- private List<List<String>> childArray;
对这两个List进行初始化,并插入一些数据
- groupArray = new ArrayList<String>();
- childArray = new ArrayList<List<String>>();
- groupArray.add("第一行");
- groupArray.add("第二行");
- List<String> tempArray = new ArrayList<String>();
- tempArray.add("第一条");
- tempArray.add("第二条");
- tempArray.add("第三条");
- for(int index = 0; index <groupArray.size(); ++index)
- {
- childArray.add(tempArray);
- }
定义ExpandableListView的Adapter
- //ExpandableListView的Adapter
- public class ExpandableAdapter extends BaseExpandableListAdapter
- {
- Activity activity;
- public ExpandableAdapter(Activity a)
- {
- activity = a;
- }
- public Object getChild(int groupPosition, int childPosition)
- {
- return childArray.get(groupPosition).get(childPosition);
- }
- public long getChildId(int groupPosition, int childPosition)
- {
- return childPosition;
- }
- public int getChildrenCount(int groupPosition)
- {
- return childArray.get(groupPosition).size();
- }
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent)
- {
- String string = childArray.get(groupPosition).get(childPosition);
- return getGenericView(string);
- }
- // group method stub
- public Object getGroup(int groupPosition)
- {
- return groupArray.get(groupPosition);
- }
- public int getGroupCount()
- {
- return groupArray.size();
- }
- public long getGroupId(int groupPosition)
- {
- return groupPosition;
- }
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent)
- {
- String string = groupArray.get(groupPosition);
- return getGenericView(string);
- }
- // View stub to create Group/Children 's View
- public TextView getGenericView(String string)
- {
- // Layout parameters for the ExpandableListView
- AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, 64);
- TextView text = new TextView(activity);
- text.setLayoutParams(layoutParams);
- // Center the text vertically
- text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
- // Set the text starting position
- text.setPadding(36, 0, 0, 0);
- text.setText(string);
- return text;
- }
- public boolean hasStableIds()
- {
- return false;
- }
- public boolean isChildSelectable(int groupPosition, int childPosition)
- {
- return true;
- }
- }
最后,给定义好的ExpandableListView添加上Adapter
- ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
- expandableListView.setAdapter(new ExpandableAdapter(Main.this));
运行即可见效果~~~
----------------------------------------------------------------------------------------------------------------
Android版手风琴(ExpandableListView)
源码下载:http://files.cnblogs.com/salam/WidgetDemo.rar
ExpandableListView是Android中的手风琴,本人感觉效果相当棒。
一个垂直滚动的显示两个级别(Child,Group)列表项的视图,列表项来自ExpandableListAdapter 。组可以单独展开。
expandGroup(int groupPos) :在分组列表视图中展开一组,
setSelectedGroup(int groupPosition) :设置选择指定的组。
setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :设置选择指定的子项。
getPackedPositionGroup(long packedPosition) :返回所选择的组
getPackedPositionForChild(int groupPosition, int childPosition) :返回所选择的子项
getPackedPositionType(long packedPosition) :返回所选择项的类型(Child,Group)
isGroupExpanded(int groupPosition) :判断此组是否展开
一个接口,将基础数据链接到一个ExpandableListView。此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。
getChildId(int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。
getChildrenCount(int groupPosition) 返回在指定Group的Child数目。
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
Android中ExpandableListView的使用相关推荐
- Android中ExpandableListView控件基本使用
本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...
- Android中ExpandableListView中嵌套ListView
最近项目挺紧张,一直没有时间总结学习,今天把最近遇到的一个奇葩的设计,做一下总结.其他的好多的APP中做的通讯录都类似微信通讯录这样,但是有这样一个需求的设计. 就是分为两个组,第一个组不需要A-Z的 ...
- Android中ExpandableListView控件的用法详解
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widge ...
- Android中ExpandableListView子项单击事件没有响应
前言 此问题多半是加入了Button等可以接收单击事件和可以接收焦点的控件导致. ExpandableListView控件的单击事件的要求大多数大神都已经说过了 1.BaseExpandableLis ...
- Android之ExpandableListView
ExpandableListView可以用来表现多层级的listView,本文主要是ExpandableListView的一个简单实现 布局文件 <LinearLayout xmlns:andr ...
- Android微信通讯录界面代码,Android中使用Expandablelistview实现微信通讯录界面
之前的博文<Android 中使用ExpandableListView 实现分组的实例>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的 ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- Android中使用ExpandableListView实现好友分组
一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...
- android list 分组,Android 中使用ExpandableListView 实现分组的实例
Android 中使用ExpandableListView 实现分组 一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给 ...
最新文章
- Scala编程之闭包(closure)
- HDU 6354 Everything Has Changed(余弦定理)多校题解
- typeof做类型判断时容易犯下的错
- 用 Python脚本生成 Android SALT 扰码
- iOS开发(3)UIButton
- 隐式调用 Intent 大全
- 外网服务器搭建网站并获取域名教程
- 解决:Could not find metadata org.apache.maven.plugins:maven-archetype-plugin/maven-metadata.xml in loc
- 执行点击事件,第一次点击后,一切正常,第二次点击,执行两次,以此类推
- 解决SecureCRT登陆Linux显示中文乱码
- Office文档在线预览/在线编辑解决方案 - 毕升OfficeAPI说明
- java print int_java – 在printin输入int
- 一个成功的Jsp程序员该怎样学习JSP呢?
- ArcMAP 使用绘图工具添加注记
- Ubuntu 安装 gcc-4.9.3-64-gnu
- NanoHTTPD 获取请求ip
- Web基础配置篇(四): Mysql的配置及使用
- 在上海社保千万不要断,如果断了,一定要这样做!
- 拳皇FANS们不得不看的动画
- 浅析部分物化以及冰山立方体的计算方法Star-Cubing
热门文章
- github报错“remote: Support for password authentication was removed on August 13, 2021. Please use a p”
- 【OpenGL】十一、OpenGL 绘制多个点 ( 绘制单个点 | 绘制多个点 )
- 【Android 内存优化】自定义组件长图组件 ( 长图滚动区域解码 | 手势识别 GestureDetector | 滑动计算类 Scroller | 代码示例 )
- html字符实体和实体名称 lt;
- redis as session_handler
- bzoj 3223: Tyvj 1729 文艺平衡树
- 手机数据抓包以及wireshark技巧
- C#程序员干货系列之语音识别
- Vs2005 正在更新 IntelliSense无法通过的解决办法
- LIbGDX 示例Tests详解一:AccelerometerTest