Android ExpandableListView的使用
一、MainActivity要继承ExpandableListActivity。效果是当单击ListView的子项是显示另一个ListView。
package com.example.explear; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.transition.SidePropagation;
import android.widget.SimpleExpandableListAdapter; public class MainActivity extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private SimpleExpandableListAdapter eListAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childDataList = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 10; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group" + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even"
: "this group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 8; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child" + i);
curChildMap.put(IS_EVEN, (i % 2 == 0) ? "This chile is even"
: "this chile is odd");
}
childDataList.add(children);
}
eListAdapter = new SimpleExpandableListAdapter(this, groupData,
android.R.layout.simple_expandable_list_item_2, new String[] {
NAME, IS_EVEN }, new int[] { android.R.id.text1,
android.R.id.text2 }, childDataList,
android.R.layout.simple_expandable_list_item_2, new String[] {
NAME, IS_EVEN }, new int[] { android.R.id.text1,
android.R.id.text2 });
setListAdapter(eListAdapter);
}
}
1、groupData是父数据 ,childDataList是子数据。
2、android.R.layout.simple_expandable_list_item_2表示list的实现方式
3、new String[] { NAME,IS_EVEN} list需要显示的数据的键值,真正显示的是键对应的值
效果图:
二、数据源:SimpleCursorTreeAdapter的使用
package com.example.explear; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.transition.SidePropagation;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.SimpleExpandableListAdapter; public class MainActivity extends ExpandableListActivity {
private int mGroupIdColumnIndex;
private String mPhoneNumberProjection[] = new String[] { People.Phones._ID,
People.Phones.NUMBER }; private ExpandableListAdapter mAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 搜索
Cursor groupCursor = managedQuery(People.CONTENT_URI, new String[] {
People._ID, People.NAME }, null, null, null);
// 创建ID 列索引
mGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow(People._ID); // 创建adapter
mAdapter = new MyExpandableListAdapter(groupCursor, this,
android.R.layout.simple_expandable_list_item_1,
android.R.layout.simple_expandable_list_item_1,
new String[] { People.NAME }, new int[] { android.R.id.text1 },
new String[] { People.NUMBER },
new int[] { android.R.id.text1 });
setListAdapter(mAdapter);
}; public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
public MyExpandableListAdapter(Cursor cursor, Context context,
int groupLayout, int childLayout, String[] groupFrom,
int[] groupTo, String[] childrenFrom, int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
} @Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
// 传进来一个组,返回一个对组内所有子组的cursor
// 返回一个指向这个联系人电话的箭头
Uri.Builder builder = People.CONTENT_URI.buildUpon();
ContentUris.appendId(builder,
groupCursor.getLong(mGroupIdColumnIndex));
builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY);
Uri phoneNumberUri = builder.build(); return managedQuery(phoneNumberUri, mPhoneNumberProjection, null,
null, null); }
}
}
效果图
三、通过BaseExpandableListAdapter绑定数据
activity_main.xml
<LinearLayout 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:orientation="vertical"
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="com.example.explear.MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" /> <ExpandableListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ExpandableListView> </LinearLayout>
注意这里的ExpandableListView的id设置需要使用@android:id/list,否则会报
Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'错误
package com.example.explear; import android.R.anim;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends ExpandableListActivity {
private String[] groups = { "group1", "group2", "group3", "group4" };
private String[][] children = {
{ "g1 item1", "g1 item2", "g1 item3", "g1 item4" },
{ "g2 item1", "g2 item2", "g2 item3", "g2 item4" },
{ "g3 item1", "g3 item2" }, { "g4 item1", "g4 item2" } }; private ExpandableListView expandableListView;
private TextView tView = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tView = (TextView) findViewById(R.id.textView1);
expandableListView = (ExpandableListView) findViewById(android.R.id.list); BaseExpandableListAdapter adapter = new BaseExpandableListAdapter() { @Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
String string = groups[groupPosition]
+ children[groupPosition][childPosition];
tView.setText(string);
return true;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(0);
layout.setPadding(50, 0, 0, 0);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.ic_launcher);
layout.addView(imageView); TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
layout.addView(textView);
return layout;
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public int getGroupCount() {
return groups.length;
} @Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
} @Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
} @Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition)
.toString()); return textView;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
};
expandableListView.setAdapter(adapter);
} protected TextView getTextView() {
AbsListView.LayoutParams lParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lParams);
textView.setPadding(20, 0, 0, 0);
textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
return textView;
}
}
效果图:
Android ExpandableListView的使用的更多相关文章
- 【开源项目4】Android ExpandableListView
如果你对Android提供的Android ExpandableListView并不满意,一心想要实现诸如Spotify应用那般的效果,那么SlideExpandableListView绝对是你最好的 ...
- 【原创】Android ExpandableListView使用
ExpandableView的使用可以绑定到SimpleExpandableListAdapter,主要是看这个Adapter怎么用. 这个类默认的构造函数有9个参数, 很好地解释了什么叫做又臭又长. ...
- Android ExpandableListView
ExpandableListView 结合SimpleExpandableListAdapter用法 最终实现效果: activity_main.xml <?xml version=" ...
- android ExpandableListView详解
ExpandableListView是android中可以实现下拉list的一个控件,是一个垂直滚动的心事两个级别列表项手风琴试图,列表项是来自ExpandableListViewaAdapter,组 ...
- 解决android expandablelistview 里面嵌入gridview行数据重复问题
最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字. 很简单 ...
- Android ExpandableListView的下拉刷新实现
该控件的修改时根据PullToRefreshList的机制修改 下面是对ExpandableListView的扩展 package com.up91.gwy.view.componet; import ...
- Android ExpandableListView的技巧和问题
前言: 最近一个多月在认真的学习Android和做项目,文章内容表达的不好或者理解错了,希望大家评论指出. :-) 本文是总结几个比较常用且使用的技巧,和一个大家都会遇到的问题. 文章中大部分语句摘抄 ...
- Android ExpandableListView 带有Checkbox的简单应用
expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> < ...
- Android ExpandableListView的简单应用
Expandablelistview1Activity.java package com.wangzhu.demoexpandablelistview; import java.util.ArrayL ...
- Android ExpandableListView使用+获取SIM卡状态信息
ExpandableListView 是一个可以实现下拉列表的控件,大家可能都用过QQ,QQ中的好友列表就是用ExpandableListView实现的,不过它是自定义的适配器.本篇 博客除了要介绍E ...
随机推荐
- N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...
- 学习动态性能表(17)--v$segstat&v$segment_statistics
学习动态性能表 第17篇-(1)-V$SEGSTAT 2007.6.13 本视图实时监控段级(segment-level)统计项,支持oracle9ir2及更高版本 V$SEGSTAT中的常用列 T ...
- bzoj 3527 [Zjoi2014]力——FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 把 q[ i ] 除掉.设 g[ i ] = i^2 ,有一半的式子就变成卷积了:另一 ...
- BZOJ2141:排队
浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- Northwind 示例数据库
Northwind 示例数据库 Northwind Traders 示例数据库包含一个名为 Northwind Traders 的虚构公司的销售数据,该公司从事世界各地的特产食品进出口贸易. 下载地址 ...
- 在CentOS上安装Java开发环境:使用yum安装jdk
请参考百度经验:http://jingyan.baidu.com/article/4853e1e51d0c101909f72607.html 如果您阅读过此文章有所收获,请为我顶一个,如果文章中有错误 ...
- 技术总监Sycx的故事
其实我在各种演讲里,线下吹牛里面无数次提及过他,讲过他的故事,但是总还是没有任何一次认认真真的详细讲过,所以,今天就讲讲他的故事吧. Sycx是福建漳州人,我经常开玩笑说,你生于一个著名的骗子之乡,为 ...
- VerilogHDL编译预处理
编译预处理语句 编译预处理是VerilogHDL编译系统的一个组成部分,指编译系统会对一些特殊命令进行预处理,然后将预处理结果和源程序一起在进行通常的编译处理.以”`” (反引号)开始的某些标识符是编 ...
- 【转】 Pro Android学习笔记(八四):了解Package(3):包间数据共享
目录(?)[-] 共享User ID的设置 共享资源例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowing ...
- 基于OpenCV的火焰检测(三)——HSI颜色判据
上文向大家介绍了如何用最简单的RGB判据来初步提取火焰区域,现在我要给大家分享的是一种更加直观的判据--HSI判据. 为什么说HSI判据是更加直观的判据呢?老规矩,先介绍一下HSI色彩模型: HSI颜 ...