ExpandableListView

package com.foxconn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListViewDemo extends Activity {
private ExpandableListView exlv;
private List<Map<String, Contact>> groups;
private List<Map<String, List<Contact>>> childs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandablelistview);
exlv = (ExpandableListView) findViewById(R.id.exlv_list);
groups = new ArrayList<Map<String, Contact>>();
childs = new ArrayList<Map<String,List<Contact>>>();
initGroupsData();

for(int i = 0; i < groups.size();i++){
initChildsData();
}


ContactJoinAdapter mAdapter = new ContactJoinAdapter(this,groups,childs);
exlv.setAdapter(mAdapter);
exlv.setClickable(true);

exlv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Looper.prepare();
Toast.makeText(ExpandableListViewDemo.this, groups.get(groupPosition).get("group").getName(), Toast.LENGTH_SHORT).show();
// Looper.loop();
System.out.println("==>"+groupPosition);
return false;
}
});

exlv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(ExpandableListViewDemo.this, childs.get(groupPosition).get("child").get(childPosition).getName(), Toast.LENGTH_SHORT).show();
System.out.println("-->"+groupPosition+"-"+childPosition);

return false;
}
});


}

private void initChildsData() {

Map<String, List<Contact>> m1 = new HashMap<String, List<Contact>>();
List<Contact> list1 = new ArrayList<ExpandableListViewDemo.Contact>();
Contact c1 = new Contact("李1", R.drawable.e);
Contact c2 = new Contact("炸彈2", R.drawable.f);
Contact c3 = new Contact("的成3", R.drawable.g);
list1.add(c1);
list1.add(c2);
list1.add(c3);
m1.put("child", list1);
childs.add(m1);


}

private void initGroupsData() {

Contact c1 = new Contact("李", R.drawable.a);
Contact c2 = new Contact("炸彈", R.drawable.b);
Contact c3 = new Contact("的成", R.drawable.c);
Map<String, Contact> m1 = new HashMap<String, Contact>();
Map<String, Contact> m2 = new HashMap<String, Contact>();
Map<String, Contact> m3 = new HashMap<String, Contact>();

m1.put("group", c1);
m2.put("group", c2);
m3.put("group", c3);

groups.add(m1);
groups.add(m2);
groups.add(m3);

}

public class ContactJoinAdapter extends BaseExpandableListAdapter{
private Context context;
private List<Map<String, Contact>> mgroups;
private List<Map<String, List<Contact>>> mchilds;
private ChildView cv;
private GroupView gv;
private LayoutInflater inflater;

public class GroupView{
public ImageView gphoto;
public TextView gname;
public TextView gcount;
}

public class ChildView{
public ImageView cphoto;
public TextView cname;
public ImageButton check;
public ImageButton uncheck;

}

public ContactJoinAdapter(Context c,List<Map<String, Contact>> groups,List<Map<String, List<Contact>>> childs) {
inflater = LayoutInflater.from(c);
cv = new ChildView();
gv = new GroupView();
this.mgroups = groups;
this.mchilds = childs;
this.context = c;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return mchilds.get(groupPosition).get("child").get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {

return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

View cview = inflater.inflate(R.layout.exchild, null);
cv.cphoto = (ImageView) cview.findViewById(R.id.im_cphoto);
cv.cname = (TextView) cview.findViewById(R.id.tv_cname);
cv.check = (ImageButton) cview.findViewById(R.id.ibtn_check);
cv.uncheck = (ImageButton) cview.findViewById(R.id.ibtn_uncheck);

cv.cphoto.setImageResource(mchilds.get(groupPosition).get("child").get(childPosition).getPhotoId());
cv.cname.setText(childs.get(groupPosition).get("child").get(childPosition).getName());


cv.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cv.check.setVisibility(View.GONE);
cv.uncheck.setVisibility(View.VISIBLE);
}
});

cv.uncheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cv.uncheck.setVisibility(View.GONE);
cv.check.setVisibility(View.VISIBLE);
}
});



return cview;
}

@Override
public int getChildrenCount(int groupPosition) {
return mchilds.get(groupPosition).get("child").size();
}

@Override
public Object getGroup(int groupPosition) {
return mgroups.get(groupPosition).get("group");
}

@Override
public int getGroupCount() {
return mgroups.size();
}

@Override
public long getGroupId(int groupPosition) {

return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

View view = inflater.inflate(R.layout.exgroup, null);
gv.gphoto = (ImageView) view.findViewById(R.id.im_gphoto);
gv.gname = (TextView) view.findViewById(R.id.tv_gname);
gv.gcount = (TextView) view.findViewById(R.id.tv_childcount);

gv.gphoto.setImageResource(mgroups.get(groupPosition).get("group").getPhotoId());
gv.gname.setText(mgroups.get(groupPosition).get("group").getName());
gv.gcount.setText("3");
//super.onCreate(savedInstanceState);
return view;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {

return false;
}

}

public class Contact{
private String name;
private int photoId;
public Contact(String n,int i) {
this.name = n;
this.photoId = i;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhotoId() {
return photoId;
}
public void setPhotoId(int photoId) {
this.photoId = photoId;
}



}

// @Override
// public boolean onChildClick(ExpandableListView parent, View v,
// int groupPosition, int childPosition, long id) {
// Toast.makeText(this, childs.get(groupPosition).get("child").get(childPosition).getName(), Toast.LENGTH_SHORT).show();
// return true;
// }


}


expandablelistview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <ExpandableListView
       android:id="@+id/exlv_list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:drawSelectorOnTop="false"
       />
   <TextView
       android:id="@+id/join_empty"
       android:text="No Data"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:visibility="gone"
       />
</RelativeLayout>


exgroup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:id="@+id/im_gphoto"
android:layout_width="50dip"
        android:layout_height="50dip"       
        android:src="@drawable/a"
        android:layout_marginLeft="33dip"
        />
    <TextView
        android:id="@+id/tv_gname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/im_photo"
        android:layout_centerHorizontal="true"
        />
    <TextView
        android:id="@+id/tv_childcount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_name"
        android:layout_marginLeft="12dip"
        android:layout_alignParentRight="true"
        android:textColor="@android:color/darker_gray"
        />
</RelativeLayout>





exchild.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:id="@+id/im_cphoto"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/c"
        android:layout_marginLeft="50dip"
        />
    <TextView
        android:id="@+id/tv_cname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/im_photo"
        android:layout_marginLeft="8dip"
        android:layout_centerHorizontal="true"
        />
    <ImageButton
        android:id="@+id/ibtn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_name"
        android:visibility="gone"
        android:src="@drawable/check"
        android:layout_marginRight="12dip"
        android:layout_alignParentRight="true"
        />
    <ImageButton
        android:id="@+id/ibtn_uncheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_name"
        android:layout_marginRight="12dip"
        android:src="@drawable/uncheck"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>

你可能感兴趣的:(ExpandableListView)