Android:GridView在滚动时显示重复的图像

时间:2015-10-22 12:18:23

标签: android performance gridview

基本上,我已将缩略图文件名存储在数据库中,并将所有缩略图存储在名为“KinderThumbnails”的文件夹中。

理想情况下,GridView应显示所有缩略图,选择缩略图应打开一个表单,从数据库中检索与该图像相关的数据并显示。

但是,某些图像在滚动时会生成重复条目,单击时会显示空白表单。

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {

    View row = convertView;
    ViewHolder holder = new ViewHolder();
    if (row != null) {
        holder = (ViewHolder) row.getTag();
    } else {
        //inflate row and assign to holder

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.single_item_main, viewGroup, false);
        holder.cardImage = (ImageView) row.findViewById(R.id.imageView);
        holder.cardActivity = (TextView) row.findViewById(R.id.cardActivity);
        holder.cardDate = (TextView) row.findViewById(R.id.cardDate);
        row.setTag(holder);
    }

    //get each object from the Card Class and set parameters
    Card temp = list.get(i);
    File imgFile = new File("/storage/emulated/0/Pictures/KinderThumbnails/" + temp.imageFileName);

    if (imgFile.exists()) {
        Glide.with(context)
                .load(imgFile.getAbsolutePath())
                .placeholder(R.drawable.img_not_found)
                .crossFade()
                .into(holder.cardImage);

        holder.cardDate.setText(temp.date);
        holder.cardActivity.setText(temp.activityName);

    }
    return row;

}

1 个答案:

答案 0 :(得分:1)

您的条件语句if (imgFile.exists())仅在图像存在时触发,这使得回收列表项视图保持不变,因此ImageView上的drawable不会更改

如果图像不存在,您需要设置holder.cardImage的可绘制

if (imgFile.exists()) {
    Glide.with(context)
            .load(imgFile.getAbsolutePath())
            .placeholder(R.drawable.img_not_found)
            .crossFade()
            .into(holder.cardImage);

    holder.cardDate.setText(temp.date);
    holder.cardActivity.setText(temp.activityName);

} else {
    holder.cardImage.setImageResource(R.drawable.img_not_found);
    holder.cardDate.setText("");
    holder.cardActivity.setText("");
}