1、ExpandableListView作为ListView的子类,可将应用中的列表项分为几组,每组里又可以包含多个列表项。其用法与ListView类似,但是,其所显示的额列表项须有ExpandableListAdapter提供。
2、与Adapter类似,ExpandableListAdapter也有三种实现方式:
- BaseExpandableListAdapter
- SimpleCursorTreeAdapter
- SimpleExpandableListAdaptr
3、示例源码
package com.example.expandablelistviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListAdapter expandableListAdapter=new BaseExpandableListAdapter(){
int[] logos=new int[]{R.drawable.p,R.drawable.t,R.drawable.z};
private String[] armTypes = new String[]
{ "神族兵种", "虫族兵种", "人族兵种"};
private String[][] arms = new String[][]
{
{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },
{ "小狗", "刺蛇", "飞龙", "自爆飞机" },
{ "机枪兵", "护士MM" , "幽灵" }
};
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypes.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return arms[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return armTypes[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return arms[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout linearLayout=new LinearLayout(MainActivity.this);
linearLayout.setOrientation(0);
ImageView imageView=new ImageView(MainActivity.this);
imageView.setImageResource(logos[groupPosition]);
TextView textView=new TextView(MainActivity.this);
textView.setText(armTypes[groupPosition].toString());
linearLayout.addView(imageView);
linearLayout.addView(textView);
return linearLayout;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout linearLayout=new LinearLayout(MainActivity.this);
linearLayout.setOrientation(1);
LinearLayout linearLayout1=new LinearLayout(MainActivity.this);
linearLayout.setOrientation(0);
TextView textView=new TextView(MainActivity.this);
textView.setText(getChild(groupPosition,childPosition).toString());
linearLayout1.addView(textView);
linearLayout.addView(linearLayout1);
return linearLayout;
}
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return false;
}
};
ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.expandableListView);
expandableListView.setAdapter(expandableListAdapter);
}
}
4、在采用BaseExpandableListAdapter实现ExpandableListAdapter时,需要实现它的一些方法:
- getGroupConut():返回值为所包含的组列表项的数量;
- getGroupView():该方法返回的View对象将作为组列表项:
- getChildrenCount():该方法返回特定组所包含的自列表项的数量;
- getChildView():该方法返回的View将作为特定组,特定位置的自列表项;
5、实现完ExpandableListAdapter后,ExpandableListAdapter调用setAdapter(adapter)即可。