最近需要用到级联操作,一级菜单和二级菜单的显示,发现Android官方自带了一个控件,ExpandableListView,学了一下用法,还好比较简单,当然也要先写一个自定义的适配器去继承BaseExpandableListAdapter,并实现方法
public class MyAdapter extends BaseExpandableListAdapter{
@Override
public int getGroupCount() {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return 0;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
return null;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
一个个看,getGroupCount就是返回一级菜单的数目,getChildrenCount就是返回当前一级菜单下的二级菜单的数目,getGroup就是返回groupPosition所关联的data,getChild则是返回两个参数下关联的data,getGroupId直接返回groupPosition,getChildId如果没有要求也是直接返回childPosition,getGroupView和getChildView都是设置view显示的样子,可以在xml中自定义,也可以直接在代码中实例化。剩下的hasStableIds和isChildSelectable直接返回true就行。
如果要展示的数据比较简单只有一项的话可以直接使用list集合,但是如果数据比较多,比如一个子view中同时显示内容和时间两个属性,就需要在list集合中嵌套一个map集合。在使用集合的时候要考虑将group的数据和child的数据关联起来,这样才能联动显示。先假设一个需求,比如qq的联系人界面,一级界面时好友分组,只有一个textview,二级界面做的简陋一点,一个imageview和一个textview,那么我们先开始写布局文件
res/layout/group.xml 一级菜单的布局
res/layout/child.xml 二级菜单的布局
public class MyAdapter extends BaseExpandableListAdapter{
private Context mContext;
private List mGroups;
private List>> mChilds;
//构造函数中传入上下文对象,group的data,child的data
public MyAdapter(Context context,List group,List>> child){
this.mContext = context;
this.mGroups = group;
this.mChilds = child;
}
@Override
public int getGroupCount() {
return mGroups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mChilds.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mChilds.get(groupPosition).get(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) {
LinearLayout mLinearGroup = (LinearLayout) LayoutInflater
.from(mContext).inflate(R.layout.group, null);
TextView grouptext = (TextView) mLinearGroup.findViewById(R.id.group_text);
String text = mGroups.get(groupPosition);
grouptext.setText(text);
return mLinearGroup;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout mLinearChild = (LinearLayout) LayoutInflater
.from(mContext).inflate(R.layout.child, null);
ImageView childimg= (ImageView) mLinearChild.findViewById(R.id.child_img);
Bitmap bitmap = (Bitmap) mChilds.get(groupPosition).get(childPosition).get("image");
childimg.setImageBitmap(bitmap);
TextView childtext = (TextView) mLinearChild.findViewById(R.id.child_txt);
String text = (String) mChilds.get(groupPosition).get(childPosition).get("text");
childtext.setText(text);
return mLinearChild;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class MainActivity extends Activity {
private ExpandableListView mListView;
private MyAdapter mAdapter;
List group = null;
List>> child = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ExpandableListView) findViewById(R.id.expand);
initData();
mAdapter = new MyAdapter(getApplicationContext(), group, child);
mListView.setAdapter(mAdapter);
// mListView.setGroupIndicator(null); //设置箭头消失
}
/**
* 传入测试数据,实际获取方法类似
*/
private void initData() {
group = new ArrayList();
group.add("我的好友");
group.add("我的家人");
group.add("我的同学");
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
child = new ArrayList>>();
for (int i = 0; i < group.size(); i++) {
List
源码下载链接:http://download.csdn.net/detail/u013926110/8973617