ExpandableListView是listView的子类(效果类似于QQ好友界面的分组)
与Adapter类似,实现 ExpandableListView由如下三种常用方法
1.扩展BaseExpandableListAdapter实现ExpandableListAdapter
2.使用SimpleExpandableListAdapter将两个List集合包装秤ExpandableListAdapter
3.使用SimpleCursorTreeAdapter将Cursor中的数据包装成SimpleCursorTreeAdapter
<span style="font-size:14px;">public class ExpandableListViewTest extends Activity {
ExpandableListView expandableListView;
int img [] = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher , R.drawable.ic_launcher,
R.drawable. ic_launcher, R.drawable.ic_launcher };
private String name [] = new String[] { "张三", "李四" , "王五" , "赵六" , "孙七" };
private String action [][] = new String[][] { { "洼地", "除草" , "施肥" },
{ "洼地", "除草" , "施肥" }, { "洼地", "除草", "施肥" }, { "洼地" , "除草" , "施肥" },
{ "洼地", "除草" , "施肥" } };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_expandable_listview);
ExpandableListAdapter adapter = new ExpandableListAdapter() {
@Override
public long getCombinedGroupId(long groupId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false ;
}
@Override
public long getCombinedChildId(long groupId, long childId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void onGroupExpanded(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public void onGroupCollapsed(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false ;
}
//此处改为true
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true ;
}
//此处改为true
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true ;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout layout = new LinearLayout(ExpandableListViewTest.this );
layout.setOrientation(LinearLayout. HORIZONTAL);
ImageView view = new ImageView(ExpandableListViewTest.this );
view.setImageResource( img[groupPosition]);//根据getGroupId返回的groupPosition来获取图片的ID
layout.addView(view);
TextView text = new TextView(ExpandableListViewTest.this );
text.setTextSize(20);
text.setText(getGroup(groupPosition).toString()); //根据getGroup()方法来获取每一个text数据
layout.addView(text);
return layout;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
//返回父类数组的长度
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return name .length ;
}
//返回父类数组的每一个对象
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return name [groupPosition];
}
//返回每一组子选项数组的长度
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return action [groupPosition].length;
}
//返回的text作为子列表的每一列的text
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView text = new TextView(ExpandableListViewTest.this );
text.setTextSize(20);
text.setText(getChild(groupPosition, childPosition).toString());
return text;
}
//返回子列表的id
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
//从数组获取子列表的每一个数据
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return action [groupPosition][childPosition];
}
};
expandableListView = (ExpandableListView) findViewById(R.id.expandable );
expandableListView.setAdapter(adapter);
}
}
</span>