详解android 可展开(收缩)的列表ListView(ExpandableListView)
2010-05-11 16:15:00 来源:WEB开发网每天都在用QQ聊天,今天突然一想,android怎么实现列表的分组展开呢?看了看api,发现其实现过程也很简单。先看一下最终效果吧!
1、首先创建我们的Activity,继承 android.app.ExpandableListActivity,直接看代码吧。
package com.ideasandroid.sample;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import com.ideasandroid.sample.adapter.IdeasExpandableListAdapter;
/**
* @author IdeasAndroid
* 可展开(收缩)列表示例
*/
public class IdeasExpandableListView extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置列表适配器IdeasExpandableListAdapter
setListAdapter(new IdeasExpandableListAdapter(this));
}
}
2、创建适配器,继承android.widget.BaseExpandableListAdapter。
package com.ideasandroid.sample.adapter;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
/**
* @author IdeasAndroid
* 可展开(收缩)列表示例
*/
public class IdeasExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext = null;
// 测试数据,开发时可能来自数据库,网络....
private String[] groups = { "家人", "朋友", "同事" };
private String[] familis = { "老爸", "老妈", "妹妹" };
private String[] friends = { "小李", "张三", "李四" };
private String[] colleagues = { "陈总", "李工", "李客户" };
private List<String> groupList = null;
更多精彩
赞助商链接