Android关于自定义ExpandableListView样式
创建项目:ExpandableListView
运行项目效果:
布局文件
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/myExpandableListView"
android:drawSelectorOnTop="false"
/>
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="no data"
/>
</LinearLayout>
第一级条目布局
group.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/groupTo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="40px"
android:paddingTop="3px"
android:paddingBottom="3px"
android:textSize="26sp"
android:text="No data"
/>
</LinearLayout>
第二级条目布局
child.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/childTo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="50px"
android:paddingTop="5px"
android:paddingBottom="5px"
android:textSize="20sp"
android:text="No data"/>
</LinearLayout>
在drawable-mdpi中定义my_expander_group.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/ic_picture" ></item>
</selector>
MainActivity.java
package org.wwj.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;
public class MainActivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定义一个List,该List对象为一级条目提供数据
List<Map<String, String>> groups = new ArrayList<Map<String,String>>();
Map<String, String> group1 = new HashMap<String, String>();
group1.put("group", "第一组");
Map<String, String> group2 = new HashMap<String, String>();
group2.put("group", "第二组");
groups.add(group1);
groups.add(group2);
//定义一个List,该List对象为第一个一级条目提供二级条目的数据
List<Map<String, String>> child1 = new ArrayList<Map<String,String>>();
Map<String, String> child1data1 = new HashMap<String, String>();
child1data1.put("child", "第一条");
child1.add(child1data1);
Map<String, String> child1data2 = new HashMap<String, String>();
child1data2.put("child", "第二条");
child1.add(child1data2);
//定义一个List,该List对象为第二个一级条目提供二级条目的数据
List<Map<String, String>> child2 = new ArrayList<Map<String,String>>();
Map<String, String> child2data1 = new HashMap<String, String>();
child2data1.put("child", "第三条");
child2.add(child2data1);
Map<String, String> child2data2 = new HashMap<String, String>();
child2data2.put("child", "第四条");
child2.add(child2data2);
//定义一个List,该List对象存储所有的二级条目的数据
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
//生成一个SimpleExpandableListAdapter对象
//1.context
//2.一级条目的数据
//3.用来设置一级条目样式的布局文件
//4.指定一级条目数据的key
//5.指定一级条目数据显示控件的id
//6.指定二级条目的数据
//7.用来设置二级条目样式的布局文件
//8.指定二级条目数据的key
//9.指定二级条目数据显示控件的id
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this
, groups
, R.layout.group
, new String[]{"group"}
, new int[] {R.id.groupTo}
, childs
, R.layout.child
, new String[]{"child"}
, new int[] {R.id.childTo}
);
//将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity
setListAdapter(adapter);
}
}
分享到:
相关推荐
Android 中使用ExpandableListView 实现分组 一个视图显示垂直滚动两级列表中的条目。这不同于列表视图,...数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式。getChildView()和get
这个是ExpandableListView的样式改造小demo,去掉箭头等控制,自定义选中时的效果及子View选中时的效果。
书名:《Android编程入门很简单》(清华大学出版社.王勇)。 压缩打包成2部分,这是第1部分。 本书是一本与众不同的Android学习读物,是一本化繁为简,把抽象问题具体化,把复杂问题简单化的书。本书避免出现...
[四次元]android-styled-dialogs 可自定义样式的dialog.zip [四次元]Android中实现Iphone样式的AlertDialog.zip [四次元]Android实现Windows风格的Dialog.zip [四次元]dlna库源代码包.zip [四次元]gridview分页效果....
书名:《Android编程入门很简单》(清华大学出版社.王勇)。 压缩打包成2部分,这是第2部分。 本书是一本与众不同的Android学习读物,是一本化繁为简,把抽象问题具体化,把复杂问题简单化的书。本书避免出现...
|--android 对话框样式 |--android 开机启动 |--android 挪动dialog的位置 |--android 控制对话框位置 |--android 根据uri获取路径 |--android 模拟器错误 |--android 横竖屏切换 |--android 获取mac地址 |--android...
2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图(GridView)和 图像切换器(ImageSwitcher) 功能和用法 104 2.4.12 画廊视图(Gallery)的功能和 用法 107 2.5 对话框 110 ...
2.2、android-pulltorefresh 一个强大的拉动刷新开源项目,支持各种控件下拉刷新,ListView、ViewPager、WevView、ExpandableListView、GridView、ScrollView、Horizontal ScrollView、Fragment上下左右拉动刷新,...
反观Android并没有这么方便的控件,于是便自己写了一个。本控件的发布离不开,在此非常感谢作者的分享。相对于原项目,本项目进行了一些定制化,可以对侧边栏进行一些定制,甚至,可以做高级的定制。有问题可以在...
它具有与ListView的本机fastscroll相同的滚动功能,但具有更多的可自定义设计,两种指示器样式和一个始终可见的滚动条。 两种最常见的模式是popup和indicator 。 与ListView和ExpandableListView一起使用。用法可以...
2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图(GridView)和 图像切换器(ImageSwitcher) 功能和用法 104 2.4.12 画廊视图(Gallery)的功能和 用法 107 2.5 对话框 110 2.5.1 使用...
2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图(GridView)和 图像切换器(ImageSwitcher) 功能和用法 104 2.4.12 画廊视图(Gallery)的功能和 用法 107 2.5 对话框 110 2.5.1 使用...