簡體   English   中英

Android:可擴展列表視圖實現

[英]Android: expandable list view implementation

我試圖實現一個可擴展的列表視圖適配器。

我有即時通訊代表的數據

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

我覆蓋了getGroup方法很好並且可以正常工作,但是我無法覆蓋getChild方法,因為子對象是一個列表,並且不確定是否應該返回整個列表或列表中的一個成員。

編輯:全班

public class MyGroupsAdapter extends BaseExpandableListAdapter {

    private LayoutInflater mInflater;
    private Context context;

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

    public MyGroupsAdapter(HashMap<Group, List<Contact>> groupedContactList,
            Context context) {
        this.groupedContactList = groupedContactList;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Contact getChild(int groupPosition, int childPosition) {
        return getGroup(groupPosition+1).getContactsInGroup().get(childPosition+1);
    }

    @Override
    public long getChildId(int arg0, int arg1) {
        return arg0;
    }

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

        final String contactRow = (String) getChild(groupPosition,
                childPosition).getFullName();

        mInflater = LayoutInflater.from(context);

        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.groups_exp_list_view_child, null);
        }

        TextView item = (TextView) convertView.findViewById(R.id.ListItem);
        item.setText(contactRow);

        return convertView;
    }

    @Override
    public int getChildrenCount(int arg0) {
        return groupedContactList.size();
    }

    @Override
    public Group getGroup(int arg0) {
        // return
        // groupedContactList.get(HomeScreenActivity.groupsDB.getGroup(arg0+1));
        return (Group)groupedContactList.keySet().toArray()[arg0];
        //return HomeScreenActivity.groupsDB.getGroup(arg0 + 1);
    }

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

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

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

        final String groupName = getGroup(groupPosition).getName();

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.groups_exp_list_view_header, null);
        }
        TextView item = (TextView) convertView.findViewById(R.id.ListHeader);
        item.setTypeface(null, Typeface.BOLD);
        item.setText(groupName);
        return convertView;

    }

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

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}

下面的代碼為我工作:

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;
}

}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM