本文采用一个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>
包 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;
- }
- }
- }
最后,程序运行后截图如下:
相关推荐
Android之ExpandableListView控件的使用
本文采用一个Demo来展示Android中ExpandableListView控件的使用,如如何在组/子ListView中绑定数据源。直接上代码如下: 程序结构图: layout目录下的 main.xml 文件源码如下: <?xml version=1.0 encoding=...
NULL 博文链接:https://haiyang08101.iteye.com/blog/1559160
NULL 博文链接:https://geyubin.iteye.com/blog/1187831
ExpandableListView控件基本使用
关于ExpandableListView的控件,有个需求就是之展开一条,写了一个例子上传到这里,和大家交流学习
QML 中没有直接提供类似 android 的ExpandableListView二级列表控件,treeView,但是用 treeView 实在是有些不方便,并且达不到想要的效果,所以干脆用 ListView 来扩展一个。
主要为大家详细介绍了Android中ExpandableListView使用示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
前几篇文章介绍了Listview,但在实际开发中也经常会用到多层的Listview来展示数据,比如qq中的好友展示,所以这张来了解一下ExpandableListview,基本思想与Listview大致是相同的,所以用起来会比较方便。
一个ExpandableListView的例子,实现多级菜单分类展示.rar
ExpandableListView 展开列表控件 教程请看 http://blog.csdn.net/knowheart/article/details/7002079
本人博客:Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友列表的demo
记录了Android开发的学习历程,本demo为可拓展的列表控件ExpandableListView的使用方法,并含有要点整理文档。
Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友
android Expandablelistview基本控件的使用,关注博客http://himici.com/
本文实例讲述了Android中ExpandableListView的用法,ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView 代码如下:<...
用了两个item实现 ExpandableListView。