ListView组件可以为用户提供列表的显示功能,但是如果要想对这些列表数据进行分组的管理,则就需要使用android.widget.ExpandableListView组件完成
-
package org.lxh.demo;
-
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;
-
// 继承BaseExpandableListAdapter并覆写类中的抽象方法
-
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
-
public String[] groups = { "我的好友", "家人", "同事", "黑名单" };// 组名称
-
public String[][] children = {
-
{ "李兴华", "董鸣楠", "王月清", "周艳军" },
-
{ "父亲", "母亲" }, { "刘宏伟", "李祺", "刘媛" },
-
{ "票贩子", "造假商" } }; // 定义组项
-
private Context context = null; // 保存上下文对象
-
public MyExpandableListAdapter(Context context) { // 构造方法接收
-
this.context = context;
-
}
-
@Override
-
public Object getChild(int groupPosition, int childPosition) { // 取得指定的子项
-
return this.children[groupPosition][childPosition];
-
}
-
@Override
-
public long getChildId(int groupPosition, int childPosition) { // 取得子项ID
-
return childPosition;
-
}
-
public TextView buildTextView() { // 自定义方法,建立文本
-
AbsListView.LayoutParams param = new AbsListView.LayoutParams(
-
ViewGroup.LayoutParams.FILL_PARENT, 35);// 指定布局参数
-
TextView textView = new TextView(this.context);// 创建TextView
-
textView.setLayoutParams(param); // 设置布局参数
-
textView.setTextSize(15.0f); // 设置文字大小
-
textView.setGravity(Gravity.LEFT); // 左对齐
-
textView.setPadding(40, 8, 3, 3); // 间距
-
return textView;// 返回组件
-
}
-
@Override
-
public View getChildView(int groupPosition, int childPosition,
-
boolean isLastChild, View convertView, ViewGroup parent) {// 返回子项组件
-
TextView textView = buildTextView(); // 创建TextView
-
textView.setText(getChild(groupPosition,
-
childPosition).toString()); // 设置显示文字
-
return textView;
-
}
-
@Override
-
public int getChildrenCount(int groupPosition) { // 取得子项个数
-
return this.children[groupPosition].length; // 取得子项个数
-
}
-
@Override
-
public Object getGroup(int groupPosition) {// 取得组对象
-
return this.groups[groupPosition];
-
}
-
@Override
-
public int getGroupCount() { // 取得组个数
-
return this.groups.length;
-
}
-
@Override
-
public long getGroupId(int groupPosition) {// 取得组ID
-
return groupPosition;
-
}
-
@Override
-
public View getGroupView(int groupPosition, boolean isExpanded,
-
View convertView, ViewGroup parent) { // 取得组显示组件
-
TextView textView = buildTextView(); // 建立组件
-
textView.setText(this.getGroup(groupPosition).toString()); // 设置文字
-
return textView;
-
}
-
@Override
-
public boolean hasStableIds() {
-
return true;
-
}
-
@Override
-
public boolean isChildSelectable(int groupPosition, int childPosition) {
-
return true;
-
}
-
}
020719树型组件:ExpandableListView.ppt
阅读(1422) | 评论(0) | 转发(0) |