可扩展列表视图适配器getView()重复位置

时间:2015-06-25 12:36:07

标签: android android-layout expandablelistadapter

当我在下面滚动时,我的ExpandableListView的适配器重复了视图和图像的位置是我的代码。我在没有View holder的情况下使用它,并且在两种情况下都使用视图持有者我都面临这个问题。

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

    View view = convertView;
    String groupName = (String) getGroup(groupPosition);

    if (view == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inf.inflate(R.layout.group_item, null);
        holder = new ViewHolder();

        holder.arrowImage = (ImageView) view.findViewById(R.id.imageView1);
        setImageView(holder.arrowImage, groupPosition);
        view.setTag(holder);
        holder.groupName = (TextView) view.findViewById(R.id.laptop);
        holder.groupName.setTypeface(null, Typeface.BOLD);
        holder.groupName.setText(groupName);

    } else {
        holder = (ViewHolder) view.getTag();
        holder.groupName = (TextView) view.findViewById(R.id.laptop);
        holder.groupName.setTypeface(null, Typeface.BOLD);
        holder.groupName.setText(groupName);
    }

    return view;

}

public ImageView getImageView(int position) {
    return images[position];
}

public void setImageView(ImageView image, int position) {
    images[position] = image;
}

static class ViewHolder {
    ImageView arrowImage;
    TextView groupName;
}

1 个答案:

答案 0 :(得分:1)

不要按标记选择视图

使用此

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

    View view = convertView;
    String groupName = (String) getGroup(groupPosition);

    LayoutInflater inf = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inf.inflate(R.layout.group_item, null);
        holder = new ViewHolder();

        holder.arrowImage = (ImageView) view.findViewById(R.id.imageView1);
        holder.groupName = (TextView) view.findViewById(R.id.laptop);
        holder.groupName.setTypeface(null, Typeface.BOLD);
        holder.groupName.setText(groupName);
        setImageView(holder.arrowImage, groupPosition);
        view.setTag(holder);

    return view;

}

public ImageView getImageView(int position) {
    return images[position];
}

public void setImageView(ImageView image, int position) {
    images[position] = image;
}

static class ViewHolder {
    ImageView arrowImage;
    TextView groupName;
}

这将阻止图像重复并将每个视图视为新视图