您的位置:首页 > 移动开发 > Android开发

android_ExpandableListView 下拉列表使用

2015-05-29 10:41 471 查看
总结一下ExpandableListView的使用

ExpandableListView与ListView用法差不多,它也有自己的适配器,可以根据自己的需求继承BaseExpandableListAdapter,自己写适配器

先上效果图



和ListView用法差不多,从项目中摘的代码

[java] view
plaincopy





<span style="white-space:pre"> </span>inEditText = (EditText) findViewById(R.id.infusion_et_in);

e_lv = (ExpandableListView) findViewById(R.id.infusion_elv);

e_lv.setAdapter(new InfusionParentAdapter(getApplication()));

[java] view
plaincopy





public class InfusionParentAdapter extends BaseExpandableListAdapter {

private Context context;

private String[] generalsTypes = new String[] { "张三", "李四" };

private String[][] generals = new String[][] { { "1", "2", "3" },

{ "4", "5", "6" } };

public InfusionParentAdapter() {

}

public InfusionParentAdapter(Context context) {

this.context = context;

}

@Override

public Object getChild(int groupPosition, int childPosition) {

// TODO Auto-generated method stub

return generals[groupPosition][childPosition];

}

@Override

public long getChildId(int groupPosition, int childPosition) {

// TODO Auto-generated method stub

return childPosition;

}

@Override

public View getChildView(int groupPosition, int childPosition,

boolean isLastChild, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

convertView = LayoutInflater.from(context).inflate(

R.layout.list_item_child, null);

return convertView;

}

@Override

public int getChildrenCount(int groupPosition) {

// TODO Auto-generated method stub

return generals[0].length;

}

@Override

public Object getGroup(int groupPosition) {

// TODO Auto-generated method stub

return generalsTypes[groupPosition];

}

@Override

public int getGroupCount() {

// TODO Auto-generated method stub

return generalsTypes.length;

}

@Override

public long getGroupId(int groupPosition) {

// TODO Auto-generated method stub

return groupPosition;

}

@Override

public View getGroupView(int groupPosition, boolean isExpanded,

View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

convertView=LayoutInflater.from(context).inflate(R.layout.list_item_infusion_elistview, null);

return convertView;

}

@Override

public boolean hasStableIds() {

// TODO Auto-generated method stub

return false;

}

@Override

public boolean isChildSelectable(int groupPosition, int childPosition) {

// TODO Auto-generated method stub

return false;

}

}

其中parent表示扩展的ListView,Child表示下拉列表的内容,其余的跟ListView用法一样了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: