Android中ExpandableListView控件基本使用
来源:互联网 发布:手机音频编辑软件中文 编辑:程序博客网 时间:2024/06/05 11:27
装载:http://blog.csdn.net/cjjky/article/details/6903504
本文采用一个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;
- }
- }
- }
最后,程序运行后截图如下:
//----------------------------------------------------------------------------------------------
转载:http://blog.csdn.net/t12x3456/article/details/7828620
Android 之ExpandableListView几个特殊的属性
1. 设置ExpandableListView 默认是展开的:
先实例化 exListView
然后
exListView.setAdapter(exlvAdapter);
//遍历所有group,将所有项设置成默认展开
int groupCount = exListView.getCount();
for (int i=0; i<groupCount; i++) {
exListView.expandGroup(i);
};
2. 去掉ExpandableListView 默认的箭头
用到ExpandableListView时有个箭头图标系统自带的在你自定义布局也不能去掉只要设置一个属性即可,如下:
settingLists.setGroupIndicator(null); ~~~~~~~~~~~~~~~~~此处就是设置自定义的箭头图标的。置空则没有了。
也可以自定义(但是位置还是在那个地方不推荐)如下:
首先,自定义一个expandablelistviewselector.xml文件,具体内容如下:
Java代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_expanded="true" android:drawable="@drawable/expandablelistviewindicatordown" />
<item android:drawable="@drawable/expandablelistviewindicator" />
</selector>
加一句代码如下:
settingLists.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector));
大功告成
3. 将默认的箭头修改到右边显示:
1首先 ExpandableListView elistview;
elistview.setGroupIndicator(null);//将控件默认的左边箭头去掉,
2在 自定义的继承自BaseExpandableListAdapter的adapter中有一个方法
/**
* 父类view
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.i("zhaoxiong","parent view");
LinearLayout parentLayout=(LinearLayout) View.inflate(context, R.layout.wowocoupons_parent_item, null);
TextView parentTextView=(TextView) parentLayout.findViewById(R.id.parentitem);
parentTextView.setText(parentlist.get(groupPosition));
ImageView parentImageViw=(ImageView) parentLayout.findViewById(R.id.arrow);
//判断isExpanded就可以控制是按下还是关闭,同时更换图片
if(isExpanded){
parentImageViw.setBackgroundResource(R.drawable.arrow_down);
}else{
parentImageViw.setBackgroundResource(R.drawable.arrow_up);
}
return parentLayout;
}
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用 .
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用详情
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- ExpandableListView控件基本使用
- hdu1166之树状数组入门
- MySQL的information_schema的介绍
- 图像处理职位面试题汇总(4)
- 容器vector
- rtlinitializeexceptionchain
- Android中ExpandableListView控件基本使用
- 关于commons-fileupload与struts2过滤器冲突的解决方法
- 【蓝桥杯】危险系数
- 大话设计模式-工厂方法模式(学习笔记)
- 2015考研之路
- 必看专业书籍目录
- 无权图的最小路径
- phonegap=html5+js+css+jquery+Jquery Mobile.使用listview,ajax做了一个通讯录。
- JavaScript HTML DOM 事件 onchange onmouseover onmouseout