1

私のアプリはスクロール中にイメージを変えるいくつかのアイテムを見た以外は問題なく、何とかそれはリサイクルの問題だとわかっていますが、解決方法はわかりません。私はそこから考えているので、私のアダプターでいくつかのコードの変更を試みたが、私は成功しなかった。スクロール中にカスタム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(); 
    } 

ダウンロードイメージについては、私はPicassoを使用しています。ありがとうございました!

EDIT:FULLアダプタ

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で何かがだが、プレースホルダが動作しない、私はそれと間違っsoemthingをやっているかもしれません。..

+0

アダプターの完全なコードを入力してください。 ViewHolderを使用していないようです。 – drulabs

+0

@drulabs、完全なコードを投稿しました:) – Jay

答えて

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); 
+0

どこにピカソのプレースホルダーを入れますか?私のtryブロックで?または@crowjdhの直前 – Jay

+0

tryブロック(前のコードブロック)の前には、tryブロックで問題が発生した場合にプレースホルダイメージを設定できない可能性があるので、私はお勧めします。また、myImageViewをビューの所有者ではなくメンバ変数として使用しているのはなぜですか?この場合、myImageViewは新しいセルを(onBindViewHolderが呼び出されたとき)移入するたびに変更されます。 @Jay –

+0

私はこれを試した:getImgUrl = null; Picasso.with(context).load(getImgUrl).placeholder(R.drawable.no_image).fit()。into(myImageView);私はそれがbefoireブロックをキャッチしようとした、didntの仕事を配置しました。あなたは正しいです。私はImageViewをビューホールダーで再びテストします。 – Jay

0

代わりのtryおよびcatch場合は、他

 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); 
     } 
提供します
2

recyclerviewアダプターでこれらの2つのメソッドをオーバーライドします。これはあなたの問題を解決します。

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

@Override 
    public int getItemViewType(int position) { 
     return position; 
    } 
関連する問題