Reputation: 1517
The RecyclerView I am using has an imageView with background as emptyheart image initially.When touched it should change to filledheart image.I add those checked positions to ArrayList using getAdapterPosition() of RecyclerViewAdapter and similarly remove it if the arraylist contains the touched position.but when I scroll it ImageView's background get changed at random positions.
RecyclerAdapter:
public class ShopOfferRecycAdapter extends RecyclerView.Adapter<ShopOfferRecycAdapter.ShopOfferHolder> {
View view;int counter=0;ArrayList checkeditems;
@Override
public ShopOfferHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view= LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_offers_recycler_adapter,parent,false);
return new ShopOfferHolder(view);
}
@Override
public void onBindViewHolder(ShopOfferHolder holder, int position) {
DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.mobile)
.showImageForEmptyUri(R.drawable.mobile).showImageOnFail(R.drawable.mobile).cacheInMemory(true).cacheOnDisk(true)
.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();
ImageLoader.getInstance().displayImage("http://dress-trends.com/wp-content/uploads/2015/10/Fashion-clothing-for-men-spring-summer-2016-61.jpg",holder.imageView,options);
}
@Override
public int getItemCount() {
return 10;
}
public class ShopOfferHolder extends RecyclerView.ViewHolder{
ImageView imageView,fav;
public ShopOfferHolder(View itemView) {
super(itemView);
checkeditems=new ArrayList();
imageView=(ImageView) itemView.findViewById(R.id.product_img);
fav=(ImageView) itemView.findViewById(R.id.fav);
fav.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(checkeditems.size()>0&&checkeditems.contains(getAdapterPosition()+1)==true) {
checkeditems.remove(getAdapterPosition()+1);
fav.setImageResource(R.drawable.heartnofill);
Log.e("Position removed", Integer.toString(getAdapterPosition()+1));
}
else if(checkeditems.contains(getAdapterPosition())==false){
fav.setImageResource(R.drawable.heartfill);
checkeditems.add(getAdapterPosition()+1);
Log.e("Pos added",Integer.toString(getAdapterPosition()+1));
// if(checkeditems.contains(getAdapterPosition()){
// checkeditems.remove(getAdapterPosition());
// Log.e("Position removed",Integer.toString(getAdapterPosition()));
}
return false;
}
});
}
}
}
Upvotes: 2
Views: 745
Reputation: 4522
In your onBindViewHolder
where you're loading the image into ImageView
use holder.imageView.setImageBitmap(null)
like this. This will reset the ImageView and flush previously used Image immediately.
holder.imageView.setImageBitmap(null);
ImageLoader.getInstance().displayImage("http://dress-trends.com/wp-content/uploads/2015/10/Fashion-clothing-for-men-spring-summer-2016-61.jpg",holder.imageView,options);
Upvotes: 2
Reputation: 336
Try this in your onBindViewHolder method
holder.setIsRecyclable(false);
As you can understand by the name of the method, This will tell your recycler view to stop recycling your view holder.
This can cause performance issues in some cases
But looking at the getCount method in your above code snippet it seems you have less elements to deal with :)
Upvotes: 1