为什么在滚动时自定义RecyclerView中的项目图像会发生变化?

时间:2016-04-19 08:26:08

标签: android android-studio imageview android-recyclerview picasso

我的应用程序运行正常,除了我看到一些项目在滚动时更改图像,不知怎的,我知道这是一个回收问题,但我不知道如何解决它。我在我的适配器中尝试了一些代码修改,因为我认为它来自那里,但我没有成功。

 public void onBindViewHolder(CustomAdapter_VersionR.MyViewHolder holder, int position) {
    //get current product of the list
    currentProduit = productList.get(position);

    try {
        getImgUrl = currentProduit.getUrlImageList_thumb().get(0);  
        Picasso.with(context).load(getImgUrl).fit().into(myImageView);
        Log.i("INFO", "Image loaded");
    } catch (Exception e) {
        myImageView.setImageResource(R.drawable.no_image);
        Log.e("ERROR", "No image or image error");
        e.printStackTrace();
    }

对于下载图片,我使用Picasso,感谢您的帮助!

编辑:完全适应器

private Product currentProduit;
private ImageView myImageView;
private Context context;
private List<Product> productList;
private String getAgencyName, getImgUrl;
private List<String> getUrlList_Thumb;
private List<String> getUrlList_Full;
private ImageButton favorite_img_btn;
private boolean star_isClicked;


public CustomAdapter_VersionR(Context ctx, List<Product> products) {
    this.context = ctx;
    this.productList = products;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    private TextView title, descrip, price, agency, country, city, type, nbRoom;
    public MyViewHolder(View view) {
        super(view);
        //references to layout
        title = (TextView) view.findViewById(R.id.title_product);
        descrip = (TextView) view.findViewById(R.id.descrip_product);
        price = (TextView) view.findViewById(R.id.price_product);
        myImageView = (ImageView) view.findViewById(R.id.img_product);
        agency = (TextView) view.findViewById(R.id.agency_product);
        country = (TextView) view.findViewById(R.id.country_product);
        city = (TextView) view.findViewById(R.id.city_product);
        type = (TextView) view.findViewById(R.id.type_product);
        nbRoom = (TextView) view.findViewById(R.id.nbRoom_product);
    }
}


@Override
public CustomAdapter_VersionR.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_list_row, parent, false);
    Log.i("INFO", "Creation ok");

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(CustomAdapter_VersionR.MyViewHolder holder, int position) {
    //get current product of the list
    currentProduit = productList.get(position);

    try {
        getImgUrl = currentProduit.getUrlImageList_thumb().get(0); 
        Picasso.with(context).load(getImgUrl).fit().into(myImageView);
        Log.i("INFO", "Image loaded");
    } catch (Exception e) {
        myImageView.setImageResource(R.drawable.no_image);
        Log.e("ERROR", "No image or image error");
        e.printStackTrace();
    }

    holder.agency.setText(currentProduit.getNomAgence();
    holder.descrip.setText(currentProduit.getDescription());
    holder.title.setText(currentProduit.getTitre());
    holder.price.setText(currentProduit.getPrix());
    holder.nbRoom.setText(currentProduit.getNbPieces());
    holder.country.setText(currentProduit.getPays());
    holder.city.setText(currentProduit.getVille());
    holder.type.setText(currentProduit.getType_produit());
}

@Override
public int getItemCount() {
    return productList.size();
}
编辑:经过一些研究,我的onBindViewHolder中有一些东西需要添加,但占位符不起作用,我可能会对它做错了。

3 个答案:

答案 0 :(得分:6)

在recyclerview适配器中覆盖这两个方法。这解决了你的问题。

@Override
    public long getItemId(int position) {
        return position;
    }

@Override
    public int getItemViewType(int position) {
        return position;
    }

答案 1 :(得分:1)

这是因为回收的视图具有之前项目的信息。 您必须向myImageView提供占位符图像。

考虑设置占位符图像,如下所示:

myImageView.setImageResource(R.drawable.no_image);
try {
    // current try block
} catch {
    // current catch block
}

或通过毕加索提供占位符图片:

Picasso.with(context)
    .load(getImgUrl)
    .placeholder(R.drawable.user_placeholder)
    .fit()
    .into(imageView);

答案 2 :(得分:0)

而不是尝试和捕获提供if和else

       if (yourList.profileImage != null) { 
  Picasso.with(context).load(list.getProfilePicture()).into(holder.profilePictureImageView);
        }
        else {   
    Picasso.with(context).load(R.drawable.profile_image).into(holder.profilePictureImageView);
        }