列表视图中的图像重复滚动

时间:2017-04-28 11:12:35

标签: java android listview android-studio

当滚动浏览6个图像的列表视图时,前3个图像再次作为最后3个图像重复。文本也从前3个重复相同但是我的toast消息显示正确的消息,例如。 pic 1 - 6.

这是适配器类

public class CustomAdapter extends BaseAdapter {

Context context;
List<RowItem> rowItems;

CustomAdapter(Context context, List<RowItem> rowItems) {
    this.context = context;
    this.rowItems = rowItems;
}

@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

/* private view holder class */
private class ViewHolder {
    ImageView profile_pic;
    TextView pic_name;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    View mView;

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();

        holder.pic_name = (TextView) convertView.findViewById(R.id.pic_name);
        holder.profile_pic = (ImageView) convertView.findViewById(R.id.profile_pic);

        convertView.setTag(holder);

        RowItem row_pos = rowItems.get(position);

        holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
        holder.pic_name.setText(row_pos.getPic_name());

        convertView.setTag(holder);



    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

}

我的主要活动

public class MainActivity extends Activity implements OnItemClickListener {

String[] pic_names;
TypedArray profile_pics;

List<RowItem> rowItems;
ListView mylistview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rowItems = new ArrayList<RowItem>();
    pic_names = getResources().getStringArray(R.array.pic_names);
    profile_pics = getResources().obtainTypedArray(R.array.profile_pics);

    for (int i = 0; i < pic_names.length; i++) {
        RowItem item = new RowItem(pic_names[i], profile_pics.getResourceId(i, -1));
        rowItems.add(item);
    }

    mylistview = (ListView) findViewById(R.id.list);
    CustomAdapter adapter = new CustomAdapter(this, rowItems);
    mylistview.setAdapter(adapter);

    mylistview.setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {

    String pic_name = rowItems.get(position).getPic_name();
    Toast.makeText(getApplicationContext(), "" +  pic_name,
            Toast.LENGTH_SHORT).show();
}





}

这些是吸气剂和制定者

public class RowItem {

private String pic_name;
private int profile_pic_id;

public RowItem(String pic_name, int profile_pic_id) {

    this.pic_name = pic_name;
    this.profile_pic_id = profile_pic_id;

}

public String getPic_name() {
    return pic_name;
}

public void setPic_name(String pic_name) {
    this.pic_name = pic_name;
}

public int getProfile_pic_id() {
    return profile_pic_id;
}

public void setProfile_pic_id(int profile_pic_id) {
    this.profile_pic_id = profile_pic_id;
}

}

3 个答案:

答案 0 :(得分:3)

如果条件

,请在外面写这些行
    RowItem row_pos = rowItems.get(position);

    holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
    holder.pic_name.setText(row_pos.getPic_name());

并从代码中删除一行,并且只保留一次,你写了两行

convertView.setTag(holder);

最终守则

public class CustomAdapter extends BaseAdapter {

        Context context;
       List<RowItem> rowItems;

       CustomAdapter(Context context, List<RowItem> rowItems) {
           this.context = context;
           this.rowItems = rowItems;
       }

       @Override
       public int getCount() {
           return rowItems.size();
       }

       @Override
       public Object getItem(int position) {
           return rowItems.get(position);
       }

       @Override
       public long getItemId(int position) {
           return rowItems.indexOf(getItem(position));
       }

       /* private view holder class */
       private class ViewHolder {
           ImageView profile_pic;
           TextView pic_name;
       }

       @Override
       public View getView(int position, View convertView, ViewGroup parent) {

           ViewHolder holder = null;

           View mView;

           LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

           if (convertView == null) {
               convertView = mInflater.inflate(R.layout.list_item, null);
               holder = new ViewHolder();

               holder.pic_name = (TextView) convertView.findViewById(R.id.pic_name);
               holder.profile_pic = (ImageView) convertView.findViewById(R.id.profile_pic);

               convertView.setTag(holder);


           } else {
               holder = (ViewHolder) convertView.getTag();
           }

               RowItem row_pos = rowItems.get(position);

               holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
               holder.pic_name.setText(row_pos.getPic_name());

           return convertView;
       }
   }

答案 1 :(得分:0)

将图像资源设置为if condition

之外
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    View mView;

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();

        holder.pic_name = (TextView) convertView.findViewById(R.id.pic_name);
        holder.profile_pic = (ImageView) convertView.findViewById(R.id.profile_pic);
        convertView.setTag(holder);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem row_pos = rowItems.get(position);
    holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
    holder.pic_name.setText(row_pos.getPic_name());

    return convertView;
}

答案 2 :(得分:0)

您的getView()方法错误。在没有回收视图的情况下,您正确地考虑了View的创建,但在向您提供回收的convertView时无法更改内容。这就是为什么图像永远不会改变的原因。

getView():

中进行以下更改
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    View mView;

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
        holder.pic_name = (TextView) convertView.findViewById(R.id.pic_name);
    holder.profile_pic = (ImageView) convertView.findViewById(R.id.profile_pic);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem row_pos = rowItems.get(position);

    holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
    holder.pic_name.setText(row_pos.getPic_name());
    return convertView;
}