BaseExpandableAdapter的convertView错位(child和group的convertview错位)问题??

kwf2030 发布于 2013/11/20 11:16
阅读 789
收藏 0

getChildView和getGroupView中的convertView会错位,group的convertview有时候传进来的是child的convertView,反之也是。这样导致必须每次重新inflate和findview,有没有人遇到过相同的问题?重写的那些方法都是正确的,怎么就会出现这样的结果。。。

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添加数据
        //Group实际上就是Children的分类
        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) {
        //显示Logo
    }

    private static class ViewHolder {
        ImageView img;
        TextView tv1;
        TextView tv2;
        TextView tv3;
    }
}

加载中
0
考拉熊
楼主的问题解决了吗?我也碰见相同的问题了
返回顶部
顶部