BaseExpandableListAdapter是ExpandableListAdapter的抽象基类,从一些数据中提供数据和视图给可折叠列表视图。
例子详解:
首先定义一个xml布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ExpandableListView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/ecpandable"
- />
- </LinearLayout>
Adapter:
public class TransExpandableListAdapter implements ExpandableListAdapter {
private Context mContext;
/**
* ----------定义数组--------------------------------------------------
*/
private int[] images = new int[]{
R.drawable.ic_launcher,
R.drawable.demo_1,
R.drawable.demo_2
};
private String[] armTypes = new String[]{
"神族", "虫族", "人族"
};
private String[][] arms = new String[][]{
{"狂战士", "龙骑士", "黑暗圣堂"},
{"小狗", "飞龙", "自爆妃子"},
{"步兵", "伞兵", "护士mm"}
};
public TransExpandableListAdapter(Context context) {
this.mContext = context;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getGroupCount() {
return armTypes.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return arms[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return armTypes[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return arms[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView textView = getTextView();//调用定义的getTextView()方法
textView.setText(getGroup(groupPosition).toString());//添加数据
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);//定义为纵向排列
ImageView logo = new ImageView(mContext);
logo.setImageResource(images[groupPosition]);//添加图片
ll.addView(logo);
TextView textView = getTextView();//调用定义的getTextView()方法
textView.setText(getChild(groupPosition, childPosition).toString());//添加数据
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void onGroupExpanded(int groupPosition) {
}
@Override
public void onGroupCollapsed(int groupPosition) {
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
//定义一个TextView
private TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 40);
TextView textView = new TextView(mContext);
textView.setLayoutParams(lp);
textView.setPadding(50, 0, 0, 0);
textView.setTextSize(16);
return textView;
}
}
Activity中:
private TransExpandableListView mListView;
private ExpandableListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trans_expandable_guo);
initView();
}
private void initView() {
mListView = (TransExpandableListView) findViewById(R.id.trans_expandable_listview);
mAdapter = new TransExpandableListAdapter(this);
mListView.setAdapter(mAdapter);
}
效果图: