Android:可扩展列表视图实现

Android:可扩展列表视图实现,android,expandablelistview,Android,Expandablelistview,我正在尝试实现一个可扩展的列表视图适配器 我有我在适配器中表示的数据 private Map<Group, List<Contact>> groupedContactList = new HashMap<Group, List<Contact>>(); 以下代码适用于我: public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context

我正在尝试实现一个可扩展的列表视图适配器

我有我在适配器中表示的数据

private Map<Group, List<Contact>> groupedContactList = new HashMap<Group, List<Contact>>();

以下代码适用于我:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private ExpandableListView mExpandableListView;
private List<GroupEntity> mGroupCollection;
private int[] groupStatus;

public ExpandableListAdapter(Context pContext,
        ExpandableListView pExpandableListView,
        List<GroupEntity> pGroupCollection) {
    mContext = pContext;
    mGroupCollection = pGroupCollection;
    mExpandableListView = pExpandableListView;
    groupStatus = new int[mGroupCollection.size()];

    setListEvent();
}


private void setListEvent() {

    mExpandableListView
            .setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int arg0) {
                    // TODO Auto-generated method stub
                    groupStatus[arg0] = 1;
                    mExpandableListView.setSelectedGroup(arg0);
                }
            });

    mExpandableListView
            .setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int arg0) {
                    // TODO Auto-generated method stub
                    groupStatus[arg0] = 0;
                }
            });
}

@Override
public String getChild(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}

@Override
public long getChildId(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
        ViewGroup arg4) {
    // TODO Auto-generated method stub

    ChildHolder childHolder;
    if (arg3 == null) {
        arg3 = LayoutInflater.from(mContext).inflate(
                R.layout.list_group_item, null);

        childHolder = new ChildHolder();

        childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
        arg3.setTag(childHolder);
    } else {
        childHolder = (ChildHolder) arg3.getTag();
    }

    childHolder.title
            .setText(mGroupCollection.get(arg0).GroupItemCollection
                    .get(arg1).Name);

    return arg3;
}

@Override
public int getChildrenCount(int arg0) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(arg0).GroupItemCollection.size();
}

@Override
public Object getGroup(int arg0) {
    return mGroupCollection.get(arg0);
}

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

@Override
public long getGroupId(int arg0) {
    return arg0;
}

@Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
    // TODO Auto-generated method stub
    GroupHolder groupHolder;
    if (arg2 == null) {
        arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,
                null);
        groupHolder = new GroupHolder();
        groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
        groupHolder.img1 = (FrameLayout) arg2.findViewById(R.id.tag_img1);
        groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
        arg2.setTag(groupHolder);
    } else {
        groupHolder = (GroupHolder) arg2.getTag();
    }
    if (groupStatus[arg0] == 0) {
        groupHolder.img.setImageResource(R.drawable.icn_max);
        groupHolder.img1
                .setBackgroundResource(R.drawable.content_bgparent1);

    } else {
        groupHolder.img.setImageResource(R.drawable.icn_min);
        groupHolder.img1.setBackgroundResource(R.drawable.active_bg);
    }
    groupHolder.title.setText(mGroupCollection.get(arg0).Name);

    return arg2;
}

class GroupHolder {
    ImageView img;
    FrameLayout img1;
    TextView title;
}

class ChildHolder {
    TextView title;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return true;
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私有上下文;
私有可扩展列表视图mExpandableListView;
私人名单收集;
私有int[]组状态;
公共ExpandableListAdapter(上下文pContext,
ExpandableListView PEExpandableListView,
列表(集合){
mContext=pContext;
mGroupCollection=pGroupCollection;
mExpandableListView=pExpandableListView;
groupStatus=newint[mGroupCollection.size()];
setListEvent();
}
私有void setListEvent(){
mExpandableListView
.setOnGroupExpandListener(新OnGroupExpandListener(){
@凌驾
public void onGroupExpand(int arg0){
//TODO自动生成的方法存根
groupStatus[arg0]=1;
mExpandableListView.setSelectedGroup(arg0);
}
});
mExpandableListView
.setOnGroupCollapseListener(新OnGroupCollapseListener(){
@凌驾
公共无效组折叠(int arg0){
//TODO自动生成的方法存根
groupStatus[arg0]=0;
}
});
}
@凌驾
公共字符串getChild(int arg0,int arg1){
//TODO自动生成的方法存根
返回mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}
@凌驾
公共长getChildId(int arg0,int arg1){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getChildView(int arg0、int arg1、布尔值arg2、视图arg3、,
视图组(arg4){
//TODO自动生成的方法存根
儿童持有人儿童持有人;
如果(arg3==null){
arg3=从(mContext)充气(
R.layout.list\u group\u item,空);
childHolder=新的childHolder();
childHolder.title=(TextView)arg3.findViewById(R.id.item_title);
arg3.设置标签(儿童持有人);
}否则{
childHolder=(childHolder)arg3.getTag();
}
子女所有权
.setText(mGroupCollection.get(arg0).GroupItemCollection
.get(arg1.Name);
返回arg3;
}
@凌驾
公共整数getChildrenCount(整数arg0){
//TODO自动生成的方法存根
返回mGroupCollection.get(arg0.GroupItemCollection.size();
}
@凌驾
公共对象getGroup(int arg0){
返回mGroupCollection.get(arg0);
}
@凌驾
public int getGroupCount(){
返回mGroupCollection.size();
}
@凌驾
公共长getGroupId(int arg0){
返回arg0;
}
@凌驾
公共视图getGroupView(整数arg0、布尔值arg1、视图arg2、视图组arg3){
//TODO自动生成的方法存根
股东;股东;
如果(arg2==null){
arg2=LayoutFlater.from(mContext)。充气(R.layout.list\u组,
无效);
groupHolder=新的groupHolder();
groupHolder.img=(ImageView)arg2.findViewById(R.id.tag\u img);
groupHolder.img1=(FrameLayout)arg2.findViewById(R.id.tag\u img1);
groupHolder.title=(TextView)arg2.findViewById(R.id.group\u title);
arg2.setTag(groupHolder);
}否则{
groupHolder=(groupHolder)arg2.getTag();
}
if(组状态[arg0]==0){
groupHolder.img.setImageResource(R.drawable.icn_max);
groupHolder.img1
.setBackgroundResource(R.drawable.content_bgparent1);
}否则{
groupHolder.img.setImageResource(R.drawable.icn_min);
groupHolder.img1.setBackgroundResource(R.drawable.active_bg);
}
groupHolder.title.setText(mGroupCollection.get(arg0.Name));
返回arg2;
}
类股东{
图像视图img;
框架布局img1;
文本视图标题;
}
班级儿童持有者{
文本视图标题;
}
@凌驾
公共布尔表ID(){
//TODO自动生成的方法存根
返回true;
}
@凌驾
公共布尔值isChildSelectable(int arg0,int arg1){
//TODO自动生成的方法存根
返回true;
}
}


希望这有帮助。

你能发布整个课程吗?@Harshid我发布了它。getChild不工作
public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private ExpandableListView mExpandableListView;
private List<GroupEntity> mGroupCollection;
private int[] groupStatus;

public ExpandableListAdapter(Context pContext,
        ExpandableListView pExpandableListView,
        List<GroupEntity> pGroupCollection) {
    mContext = pContext;
    mGroupCollection = pGroupCollection;
    mExpandableListView = pExpandableListView;
    groupStatus = new int[mGroupCollection.size()];

    setListEvent();
}


private void setListEvent() {

    mExpandableListView
            .setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int arg0) {
                    // TODO Auto-generated method stub
                    groupStatus[arg0] = 1;
                    mExpandableListView.setSelectedGroup(arg0);
                }
            });

    mExpandableListView
            .setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int arg0) {
                    // TODO Auto-generated method stub
                    groupStatus[arg0] = 0;
                }
            });
}

@Override
public String getChild(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}

@Override
public long getChildId(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
        ViewGroup arg4) {
    // TODO Auto-generated method stub

    ChildHolder childHolder;
    if (arg3 == null) {
        arg3 = LayoutInflater.from(mContext).inflate(
                R.layout.list_group_item, null);

        childHolder = new ChildHolder();

        childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
        arg3.setTag(childHolder);
    } else {
        childHolder = (ChildHolder) arg3.getTag();
    }

    childHolder.title
            .setText(mGroupCollection.get(arg0).GroupItemCollection
                    .get(arg1).Name);

    return arg3;
}

@Override
public int getChildrenCount(int arg0) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(arg0).GroupItemCollection.size();
}

@Override
public Object getGroup(int arg0) {
    return mGroupCollection.get(arg0);
}

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

@Override
public long getGroupId(int arg0) {
    return arg0;
}

@Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
    // TODO Auto-generated method stub
    GroupHolder groupHolder;
    if (arg2 == null) {
        arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,
                null);
        groupHolder = new GroupHolder();
        groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
        groupHolder.img1 = (FrameLayout) arg2.findViewById(R.id.tag_img1);
        groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
        arg2.setTag(groupHolder);
    } else {
        groupHolder = (GroupHolder) arg2.getTag();
    }
    if (groupStatus[arg0] == 0) {
        groupHolder.img.setImageResource(R.drawable.icn_max);
        groupHolder.img1
                .setBackgroundResource(R.drawable.content_bgparent1);

    } else {
        groupHolder.img.setImageResource(R.drawable.icn_min);
        groupHolder.img1.setBackgroundResource(R.drawable.active_bg);
    }
    groupHolder.title.setText(mGroupCollection.get(arg0).Name);

    return arg2;
}

class GroupHolder {
    ImageView img;
    FrameLayout img1;
    TextView title;
}

class ChildHolder {
    TextView title;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return true;
}