package com.example.android_test;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyExpandableListActivity extends ExpandableListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//创建适配器,使用BaseExpandableListAdapter
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
String[] listTitles={"列表标题1","列表标题2","列表标题3"};
String[][] listContents={
{"内容1","内容1","内容1","内容1","内容1"},
{"内容2","内容2","内容2"},
{"内容3","内容3","内容3","内容3"}
};
/**
* 是否选中指定位置上的子元素。
* 参数
* groupPosition 组位置(该组内部含有这个子元素)
* childPosition 子元素位置
* 返回值
* 是否选中子元素
*/
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public int getGroupCount() {
return listTitles.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return listContents[groupPosition].length;
}
/**
* 获取指定组中的数据
参数
groupPosition 组位置
返回值
返回组中的数据,也就是该组中的子元素数据。
*/
@Override
public Object getGroup(int groupPosition) {
return listTitles[groupPosition];
}
/**
* 获取指定组中的指定子元素数据。
* 返回值:返回指定子元素数据。
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return listContents[groupPosition][childPosition];
}
/**
* 获取指定组的ID,这个组ID必须是唯一的。联合ID(参见getCombinedGroupId(long))在所有条目(所有组和所有元素)中也是唯一的。
参数
groupPosition 组位置
返回值
返回组相关ID
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* 获取指定组中的指定子元素ID,这个ID在组里一定是唯一的。
* 参数
* groupPosition 组位置(该组内部含有子元素)
* childPosition 子元素位置(相对于其它子元素)
* 返回值
* 子元素关联ID。
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
返回值
返回一个Boolean类型的值,如果为TRUE,意味着相同的ID永远引用相同的对象。
*/
@Override
public boolean hasStableIds() {
return true;
}
/**
* 决定每个组选项的外观
* 获取显示指定组的视图对象。这个方法仅返回关于组的视图对象,要想获取子元素的视图对象,就需要调用getChildView(int, int, boolean, View, ViewGroup)。
参数
groupPosition 组位置(决定返回哪个视图)
isExpanded 该组是展开状态还是伸缩状态
convertView 重用已有的视图对象。注意:在使用前你应该检查一下这个视图对象
是否非空并且这个对象的类型是否合适。由此引伸出,如果该对象不能
被转换并显示正确的数据,这个方法就会调用
getGroupView(int, boolean, View, ViewGroup)
来创建一个视图(View)对象。
parent 返回的视图对象始终依附于的视图组。
返回值
返回指定组的视图对象
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll=new LinearLayout(MyExpandableListActivity.this);
ll.setOrientation(0);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
/**
* 获取一个视图对象,显示指定组中的指定子元素数据。
参数
groupPosition 组位置(该组内部含有子元素)
childPosition 子元素位置(决定返回哪个视图)
isLastChild 子元素是否处于组中的最后一个
convertView 重用已有的视图(View)对象。注意:在使用前你应该检查一下这个视图对象是否非空并且这个对象的类型是否合适。
由此引伸出,如果该对象不能被转换并显示正确的数据,
这个方法就会调用getChildView(int, int, boolean, View, ViewGroup)
来创建一个视图(View)对象。
parent 返回的视图(View)对象始终依附于的视图组。
返回值
指定位置上的子元素返回的视图对象
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView=getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//创建方法getTextView
private TextView getTextView() {
//AbsListView.LayoutParams.LayoutParams(int width, int height)
//Abslistview 是用于实现条目的虚拟列表的基类.
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView=new TextView(MyExpandableListActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.LEFT);
textView.setTextSize(20);
textView.setPadding(60, 0, 0, 0);
return textView;
}
};
setListAdapter(adapter);
}
}