Skip to main content
 首页 » 编程设计

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

2026年04月06日34lovecherry

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

 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 中有一些东西要添加,但是占位符不起作用,我可能做错了什么..

请您参考如下方法:

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

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