在ExpandableAdapter中混合了getChildView()和getGroupView()中的ConvertView arg?

ut_et 发布于 2019-04-15 android 最后更新 2019-04-15 22:59 16 浏览

当调用getChildView()时,convertView有时是我在getGroupView()中创建的视图,并且在调用getGroupView()时,convertView有时是我在getChildView中创建的视图,为什么会发生这种情况?

<code>
public class FavoriteAdapter extends BaseExpandableListAdapter {
    private Context ctx;
    private int[] mGroups;
    private List<Object>[] mChildren;
public FavoriteAdapter(Context ctx) {
        this.ctx = ctx;
        mGroups = new int[0];
        mChildren = new List[0];
    }
public void addAll(List<Object> list) {
        //mGroups=...
        //mChildren=...
        notifyDataSetChanged();
    }
@Override
    public int getGroupCount() {
        return mGroups.length;
    }
@Override
    public int getChildrenCount(int groupPosition) {
        return mChildren[groupPosition].size();
    }
@Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
@Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
@Override
    public int getChildTypeCount() {
        return mChildren.length;
    }
@Override
    public int getChildType(int groupPosition, int childPosition) {
        return groupPosition;
    }
@Override
    public Object getGroup(int groupPosition) {
        return mGroups[groupPosition];
    }
@Override
    public Object getChild(int groupPosition, int childPosition) {
        return mChildren[groupPosition].get(childPosition);
    }
@Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder h;
        if (null == convertView) {
            h = new ViewHolder();
            convertView = LayoutInflater.from(ctx).inflate(R.layout.group_item, null);
            h.tv1 = (TextView) convertView.findViewById(R.id.group_tv);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        switch ((Integer) getGroup(groupPosition)) {
            case Type.TYPE_1:
                h.tv1.setText("Type1");
                break;
case Type.TYPE_2:
                h.tv1.setText("Type2");
                break;
case Type.TYPE_3:
                h.tv1.setText("Type3");
                break;
case Type.TYPE_4:
                h.tv1.setText("Type4");
                break;
case Type.TYPE_5:
                h.tv1.setText("Type5");
                break;
default:
                throw new RuntimeException("Group type does not match!!!");
        }
return convertView;
    }
@Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        Object p = getChild(groupPosition, childPosition);
ViewHolder h;
        if (null == convertView) {
            h = new ViewHolder();
            switch (p.type) {
                case Type.TYPE_1:
                    convertView = LayoutInflater.from(ctx).inflate(R.layout.item_1, null);
                    h.img = (ImageView) convertView.findViewById(R.id.logo);
                    h.tv1 = (TextView) convertView.findViewById(R.id.title);
                    h.tv2 = (TextView) convertView.findViewById(R.id.intro);
                    break;
case Type.TYPE_2:
                    convertView = LayoutInflater.from(ctx).inflate(R.layout.item_2, null);
                    h.img = (ImageView) convertView.findViewById(R.id.logo);
                    h.tv1 = (TextView) convertView.findViewById(R.id.title);
                    h.tv2 = (TextView) convertView.findViewById(R.id.intro);
                    break;
case Type.TYPE_3:
                    convertView = LayoutInflater.from(ctx).inflate(R.layout.item_3, null);
                    h.tv1 = (TextView) convertView.findViewById(R.id.title);
                    h.tv2 = (TextView) convertView.findViewById(R.id.time);
                    break;
case Type.TYPE_4:
                    convertView = LayoutInflater.from(ctx).inflate(R.layout.item_4, null);
                    h.img = (ImageView) convertView.findViewById(R.id.logo);
                    h.tv1 = (TextView) convertView.findViewById(R.id.title);
                    h.tv2 = (TextView) convertView.findViewById(R.id.addr);
                    h.tv3 = (TextView) convertView.findViewById(R.id.area);
                    break;
case Type.TYPE_5:
                    convertView = LayoutInflater.from(ctx).inflate(R.layout.item_5, null);
                    h.img = (ImageView) convertView.findViewById(R.id.logo);
                    h.tv1 = (TextView) convertView.findViewById(R.id.title);
                    h.tv2 = (TextView) convertView.findViewById(R.id.addr);
                    h.tv3 = (TextView) convertView.findViewById(R.id.tel);
                    break;
default:
                    throw new RuntimeException("Child type does not match!!!");
            }
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
switch (p.type) {
            case Type.TYPE_1:
                if (null != p.thumb && p.thumb.length() != 0) {
                    displayImage(p.thumb, h.img);
                } else {
                    h.img.setVisibility(View.GONE);
                }
                if (null != p.title && p.title.length() != 0) {
                    h.tv1.setText(p.title);
                }
                if (null != p.detail && p.detail.length() != 0) {
                    h.tv2.setText(p.detail);
                }
                break;
case Type.TYPE_2:
                if (null != p.thumb && p.thumb.length() != 0) {
                    displayImage(p.thumb, h.img);
                } else {
                    h.img.setVisibility(View.GONE);
                }
                if (null != p.title && p.title.length() != 0) {
                    h.tv1.setText(p.title);
                }
                if (null != p.intro) {
                    h.tv2.setText(p.intro);
                }
                break;
case Type.TYPE_3:
                if (null != p.title && p.title.length() != 0) {
                    h.tv1.setText(p.title);
                }
                break;
case Type.TYPE_4:
                if (null != p.thumb && p.thumb.length() != 0) {
                    displayImage(p.thumb, h.img);
                } else {
                    h.img.setVisibility(View.GONE);
                }
                if (null != p.title && p.title.length() != 0) {
                    h.tv1.setText(p.title);
                }
                break;
case Type.TYPE_5:
                if (null != p.thumb && p.thumb.length() != 0) {
                    displayImage(p.thumb, h.img);
                } else {
                    h.img.setVisibility(View.GONE);
                }
                if (null != p.title && p.title.length() != 0) {
                    h.tv1.setText(p.title);
                }
                break;
default:
                throw new RuntimeException("Child type does not match!!!");
        }
return convertView;
    }
@Override
    public boolean hasStableIds() {
        return false;
    }
@Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
private void displayImage(String uri, ImageView img) {
        //display logo
    }
private static class ViewHolder {
        ImageView img;
        TextView tv1;
        TextView tv2;
        TextView tv3;
    }
}
</code>
已邀请: