代码也很简单,直接上代码啦。注释比较详细。
首先是 xml, groupitem 和 childitems 就不贴了 这个自己随便吧。
main.xml 很简单,就放了个 ExpandableListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ExpandableListView
android:id="@+id/fragment_aa"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
接下来 activity中的设置
public class MainActivity extends Activity {
private ExpandableListView ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll =(ExpandableListView)this.findViewById(R.id.fragment_aa);
/** 造的假数据 让能够显示
*
*/
List<GroupEntity> group_list = new ArrayList<GroupEntity>();
//孩子集合
List<List<ChildEntity>> chis = new ArrayList<List<ChildEntity>>();
for (int i = 0; i < 3 ; i++) {
List<ChildEntity> chile_list = new ArrayList<ChildEntity>();
ChildEntity chile = new ChildEntity();
chile.name = String.valueOf(i+"chile- name");
chile.age = String.valueOf(i+"child -age");
GroupEntity group = new GroupEntity();
group.groupName = String.valueOf(i+"group - name");
chile_list.add(chile);
group_list.add(group);
chis.add(chile_list);
if( i == 2){
List<ChildEntity> chile_list1 = new ArrayList<ChildEntity>();
ChildEntity chile1 = new ChildEntity();
chile1.name = String.valueOf(i+"chile- name");
chile1.age = String.valueOf(i+"child -age");
GroupEntity group1 = new GroupEntity();
group1.groupName = String.valueOf(i+"group - name");
chile_list.add(chile1);
chis.add(chile_list1);
}
}
//----- 上面造假数据 -------------------------------------------------------------------
// 初始化适配器
ExpandInfoAdapter1 adapter1 = new ExpandInfoAdapter1(this,
group_list,chis);
//设置背景
ll.setBackgroundResource(R.drawable.online_bj);
//指示器
ll.setChildIndicator(getResources().getDrawable(R.drawable.ic_launcher));
ll.setIndicatorBounds(150, 0);
//group 用自己的指示器 设置默认展开哪一行
ll.setGroupIndicator(null);ll.expandGroup(0);
// listView.setGroupIndicator(getResources().getDrawable(R.drawable.arrow_list2));
ll.setAdapter(adapter1);
/**
* 提供的
*/
ll.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP )
{
menu.add(1, 0, 0, "地图查看" );
menu.add(2, 0, 1, "数据查看" );
}
}
});
/**
* 自己实现的样子 当长按时
*/
// ll.setOnItemLongClickListener(new OnItemLongClickListener() {
//
// @Override
// public boolean onItemLongClick(AdapterView<?> arg0, View view,
// int position, long id) {
// Toast.makeText(MainActivity.this, "position=="+position+"==id=="+ id, 1).show();
//
// return true;
//
//
//
// }
//
// });
}
//点击事件 处理
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getGroupId() < 3) {
ExpandableListView.ExpandableListContextMenuInfo info_del =(ExpandableListView.ExpandableListContextMenuInfo)item.getMenuInfo();
switch (item.getOrder()) {
case 0:
Intent it = new Intent();
Toast.makeText(MainActivity.this, "position==0", 1).show();
break;
case 1:
Toast.makeText(MainActivity.this, "position==1", 1).show();
break;
default:
break;
}
}
return super.onContextItemSelected(item);
}
}
接下来是 适配器:
public class ExpandInfoAdapter1 extends BaseExpandableListAdapter {
private List<GroupEntity> group_list; //组
private List<List<ChildEntity>> child_list; //child
private LayoutInflater mLayoutInflater;
private Context context;
public ExpandInfoAdapter1(Context mContext, List<GroupEntity> group_list, List<List<ChildEntity>> child_list) {
this.context = mContext;
this.group_list =group_list;
this.child_list = child_list;
this.mLayoutInflater = LayoutInflater.from(mContext);
}
//得到每个“组”下面对应的 有多少条数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return child_list.get(groupPosition).get(groupPosition);
}
@Override
public long getChildId(int groupID, int groupPosition) {
return 0;
}
//子界面布局
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//加载子布局界面
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.listview_child_item,
null);
}
//得到子布局信息
final ChildEntity childEntity = child_list.get(groupPosition).get(childPosition);
LinearLayout groupItem = (LinearLayout) convertView.findViewById(R.id.ll_child_item);
// name
TextView name= (TextView) convertView.findViewById(R.id.tv_name);
name.setText(childEntity.name);
//age
TextView age= (TextView) convertView.findViewById(R.id.tv_age);
age.setText(childEntity.age);
// convertView.setTag(R.id.ll_SO2, groupPosition);
return convertView;
}
//每个group view 下面显示多少个 chile view
@Override
public int getChildrenCount(int groupID) {
return child_list.get(groupID).size();
}
//得到每个组
@Override
public Object getGroup(int groupPosition) {
return group_list.get(groupPosition);
}
//多少个组
@Override
public int getGroupCount() {
return group_list.size();
}
@Override
public long getGroupId(int groupID) {
return 0;
}
/**
* group
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.listview_group_item,
null);
}
GroupEntity model = group_list.get(groupPosition);
//得到linearlayout
LinearLayout groupItem = (LinearLayout) convertView.findViewById(R.id.ll_group_itme);
//groupItem.setTag(R.id.ll_group_itme, 1);
//设置group
TextView groupName = (TextView) convertView.findViewById(R.id.tv_name);
groupName.setText(model.groupName);
//自己设置的指示器
ImageView image = (ImageView)convertView.findViewById(R.id.adapter_listview_group_image);
if (isExpanded) {
//展开时显示的图片
image.setBackgroundResource(R.drawable.widget_expander_ic_maximized);
}else{
//收起时显示的图片
image.setBackgroundResource(R.drawable.widget_expander_ic_minimized);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
或者设置指示器 写到 drawable 也是可以的,就不用写在 adapter里面了。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_expanded="true" android:drawable="@drawable/ic_launcher" />
<item android:drawable="@drawable/ic_launcher"></item>
</selector>
再添加1个 工具类,
这个的用处就是 当你直接有 组List <entity> 和 子 List<entity> 不用再次封装 成 子 List<List<entity>>了直接传入,在adapter中使用就好了。
public class SetExpandGroupArray{
private List<GroupEntity> group_list;
private List<ChildEntity> child_list;
public SetExpandGroupArray(List<GroupEntity> group_list, List<ChildEntity> child_list){
this.group_list = group_list;
this.child_list = child_list;
}
//取出 group_list 每个组头信息
public GroupEntity getGroupEntity(int groupID){
return groupID < group_list.size() ? group_list.get(groupID) : null;
}
//copy
private void copy(List<ChildEntity> list_child, List<ChildEntity> res, int start, int end, int index){
list_child.add(res.get(index));
}
//根据 group position child postion 返回相应的数据
public ChildEntity getChild(int groupID, int groupPosition){
ChildEntity selectedContact = getGroup(groupID).get(groupPosition);
return selectedContact != null ? selectedContact : null;
}
//根据组 ID 返回 child_list
public List<ChildEntity> getGroup(int groupID){
if (!(groupID < group_list.size())){
return null;
} else{
List<ChildEntity> contactGroup = new ArrayList<ChildEntity>();
copy(contactGroup, child_list, 0, 0,groupID);
return contactGroup;
}
}
public int getGroupCount(){
return group_list.size();
}
//返回每个组下 的 数据的 size
public int getChildrenCountgetChildrenCount(int groupID){
return getGroup(groupID).size();
}
}
adapter类中的 参数把上面的工具类(已经塞入过 组list数据 和 子list数据) 对象传入就好了。可以直接用里面的方法了
public ExpandInfoAdapter(Context mContext, GetAirStationDateGroupArray list) {
this.context = mContext;
this.mCitySiteEntryGroupArray = list;
this.mLayoutInflater = LayoutInflater.from(mContext);
}