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

Jay*_*Jay 2 android imageview android-studio picasso android-recyclerview

我的应用程序工作正常,但我看到了一些项目改变图像滚动时,不知何故,我知道这是一个循环的问题,但我不知道如何解决它.我在我的适配器中尝试了一些代码修改,因为我认为它来自那里,但我没有成功.

 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();
    }
Run Code Online (Sandbox Code Playgroud)

对于下载图像,我正在使用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();
}
Run Code Online (Sandbox Code Playgroud)

编辑:经过一些研究,我的onBindViewHolder中有一些东西要添加,但占位符不起作用,我可能会做错了它...

Fat*_* km 7

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

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

@Override
    public int getItemViewType(int position) {
        return position;
    }
Run Code Online (Sandbox Code Playgroud)