介绍:
ExpandableListView是ListView的子类,它把应用中的列表项分为几组,每组里面又可以包含多个列表项。而所显示的列表项应该由ExpandableListAdapter提供。
ExpandableListAdapter常用的使用方法是通过扩展BaseExpandablelistAdapter来实现。
关键是实现如下四个方法:
(1)getGroupCount():返回包含的组列表项的数量;
(2)getGroupView():返回View对象将其作为组列表项;
(3)getChildrenCount():返回特定组所包含的子列表项的数量;
(4)getChildView():返回的View对象将作为特定组的特定位置的子列表项。
程序源码:
package com.example.expandablelistviewui;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
//
private String []creatureTypes = new String[]{"Color","People","Animal"};
private String [][]creatures = new String[][]{
{"yellow","purple","red"},
{"girl","libai","qingzhao","nongyu"},
{"tiger"}
};
//group view
private int[] grouplogo = new int[]{R.drawable.color,R.drawable.person,R.drawable.animal};
//child view
private int[][] generallogos = new int[][]{
{R.drawable.color1,R.drawable.color2,R.drawable.color3},
{R.drawable.person1,R.drawable.person2,R.drawable.person3,R.drawable.person5},
{R.drawable.animal1}};
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//key construct myself adapter
ExpandableListAdapter adapter = new BaseExpandableListAdapter(){
@Override
public Object getChild(int groupPosition, int childPosition){
return creatures[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition){
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition){
return creatures[groupPosition].length;
}
//****self define a method to display text content [***Set TextView UI]
private TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(50, 0, 0, 0);//左边距 50 【远离图标】
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
决定每一个子选项的外观 调用前面定义的getTextView 和getChild methods
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){
//add picture ---addView
LinearLayout childLayout = new LinearLayout(MainActivity.this);
childLayout.setOrientation(0);
ImageView childimage = new ImageView(MainActivity.this);
childimage.setPadding(30, 0, 0, 0);
//adjust size 1403
/*childimage.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));*/
childimage.setLayoutParams(new LayoutParams(100,100));
//childimage.setScaleType(ScaleType.FIT_CENTER);
//
childimage.setImageResource(generallogos[groupPosition][childPosition]);
childLayout.addView(childimage);
//add text ---addView
TextView textView = this.getTextView();
textView.setText(this.getChild(groupPosition, childPosition).toString());
childLayout.addView(textView);
return childLayout;
}
//begin deal with group info
@Override
public Object getGroup(int groupPosition){
return creatureTypes[groupPosition];
}
@Override
public int getGroupCount(){
return creatureTypes.length;
}
@Override
public long getGroupId(int groupPosition){
return groupPosition;
}
@Override
//决定每个组选项的外观
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent){
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);//0. set orientation
ImageView logo = new ImageView(MainActivity.this);
//1403
/*
logo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
*/
logo.setLayoutParams(new LayoutParams(150,150));
logo.setPadding(20, 0, 0, 0);//left top right bottom 边距
// logo.setScaleType(ScaleType.FIT_XY);
logo.setImageResource(grouplogo[groupPosition]);
ll.addView(logo);//1. add group image (IMageView)
TextView textView = getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);//2. add group name (textView)
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition){
return true;
}
@Override
public boolean hasStableIds(){
return true;
}
};
//
ExpandableListView lstview = (ExpandableListView)findViewById(R.id.list);
lstview.setAdapter(adapter);
//set clickEvent 1403
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ExpandableListActivity与ExpandableListActivity的用法基本相似,只要为该Activity传入一个ExpanableListAdapter对象即可,接下来的ExpandableListActivity将会生成一个显示可展开列表的窗口。
对应的代码修改片段(黑色为修改前,红色为修改后):
(1)
//public class MainActivity extends Activity {
public class MainActivity extends ExpandableListActivity {
(2)
setContentView(R.layout.activity_main);
// setContentView(R.layout.activity_main);
(3)
ExpandableListView lstview = (ExpandableListView)findViewById(R.id.list);
lstview.setAdapter(adapter);
/*
ExpandableListView lstview = (ExpandableListView)findViewById(R.id.list);
lstview.setAdapter(adapter);*/
setListAdapter(adapter);//设置该窗口显示列表
运行结果: