快速滚动后,RecyclerView显示错误的图像

时间:2016-06-09 08:24:50

标签: android imageview android-recyclerview

在我的应用程序中,我使用RecyclerView.Adapter和我的自定义Holder类。每个项目包含少量TextView和一个ImageView(图标)。问题是RecyclerView在快速滚动后显示错误的图像。

示例:

truth

NOT truth

所以,我的持有人是:

public class CafeHolder extends RecyclerView.ViewHolder implements OnClickListener {
TextView itemName, itemType, itemMarksCount, itemCommentsCount, itemRating;
ImageView imgCafe;
ImageLoader imgLoader;
private Context context;
private int cafeId;

public CafeHolder(View view, Context context){
    super(view);
    this.context = context;
    itemName = (TextView) view.findViewById(R.id.tvCafeName);
    imgCafe = (ImageView) view.findViewById(R.id.imgCafe);
    itemType = (TextView) view.findViewById(R.id.tvCafeType);
    itemMarksCount = (TextView) view.findViewById(R.id.tvMarksCount);
    itemCommentsCount = (TextView) view.findViewById(R.id.tvCommentsCount);
    itemRating = (TextView) view.findViewById(R.id.tvRatingCount);
    imgLoader = new ImageLoader(context);
    view.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Intent intent = new Intent(context, InfoActivity.class);
    intent.putExtra(CafeDomain.ID, cafeId);
    context.startActivity(intent);
}

public void setCafeId(int cafeId){
    this.cafeId = cafeId;
}

我的RecyclerView.Adapter:

public class CafeAdapter extends RecyclerView.Adapter<CafeHolder> {
private Context context;
List<CafeDomain> listItems;

public CafeAdapter(List<CafeDomain> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}

@Override
public CafeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_cafe_item2, parent, false);
    CafeHolder vh = new CafeHolder(v, context);
    return vh;
}

@Override
public void onBindViewHolder(CafeHolder holder, int position) {
    CafeDomain dItem =  this.listItems.get(position);
    holder.imgCafe.setImageDrawable(null);
    holder.itemName.setText(dItem.getName());
    ArrayList<TypeDomain> types = dItem.getTypes();
    StringBuilder sbTypes = new StringBuilder();
    for (int i = 0; i < types.size(); i++){
      sbTypes.append(types.get(i).getName());
      if (i!=types.size()-1)
          sbTypes.append(", ");  
    }
    holder.itemType.setText(sbTypes.toString());
    if (!dItem.getIconUrl().isEmpty()){
        holder.imgLoader.DisplayImage(dItem.getIconUrl(), holder.imgCafe); //load image from cache or web
    }
    else{
        holder.imgCafe.setImageResource(R.drawable.icon200); //set default image
    }


    holder.itemMarksCount.setText(String.valueOf(dItem.getMarksCount()));
    holder.itemCommentsCount.setText(String.valueOf(dItem.getCommentsCount()));
    holder.itemRating.setText(String.valueOf(dItem.getRating()));
    holder.setCafeId(listItems.get(position).getId());
}

@Override
public int getItemCount() {
    return listItems.size();
}

}

我可以看到,这段代码:

else{
        holder.imgCafe.setImageResource(R.drawable.icon200); //set default image
    }

正在运作,但无论如何两张图片都是错误的:(

更新: 问题是只有在磁盘中没有图像缓存的情况下快速滚动列表时。当我们有缓存时,这里没有问题......

1 个答案:

答案 0 :(得分:4)

嗨,我认为你的问题在这里:

    <rewrite>
         <rules>
             <rule name="Redirect to non-www" topProcessing="true">             
        <match url="(.*)" negate="false"></match>
                 <action type="Redirect" url="http://domain.com/{R:1}"></action> 
                <conditions> 
                        <add input="{HTTP_HOST}" pattern="^domain\.com$" negate="true"></add>
                 </conditions>  
           </rule>     
    </rules> 
</rewrite>

我可以为您提供两种可行的解决方案。首先使用 if (!dItem.getIconUrl().isEmpty()){ holder.imgLoader.DisplayImage(dItem.getIconUrl(), holder.imgCafe); //load image from cache or web } else{ holder.imgCafe.setImageResource(R.drawable.icon200); //set default image }

检查您的imageUrl是否为空
TextUtils.isEmpty()

如果这不起作用,如果您可以为 if (!TextUtils.isEmpty(dItem.getIconUrl())){ holder.imgLoader.DisplayImage(dItem.getIconUrl(), holder.imgCafe); } else{ holder.imgCafe.setImageResource(R.drawable.icon200); //set default image } empty url设置可绘制的图片,只需将其留给您的图片下载器,然后使用下面的代码:

url loading fail