Image is not shown in the corresponding item

1

I am using a RecyclerView to show a list in which each item contains text and images (I will search for the image from a url with the ImageLoader in the method private void setImageNews(final ImageView view, String url) ). When loading the same, the images corresponding to each item are loaded correctly. But when you scroll down and back up the images are loaded into the items that do not correspond or are not displayed. My guess is that when you scroll and send to find the image of the corresponding item, when this item loses visibility due to the sliding of the list, the Bitmap obtained from the Response is loaded in the item that takes its place in that moment. If so, what is the correct way to handle it or its solution ?, and if not, What am I doing wrong? In the onErrorResponse hidden the RelativeLayout that contains both the ImageView and a ProgressBar that is displayed while searching for the image.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter .ViewHolder> {
private ArrayList<DtoNews> mData;
private Context mContext;
private  View mView;

public MyAdapter() {
    mData = new ArrayList();
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    mContext = parent.getContext();
    mView = LayoutInflater.from(mContext)
            .inflate(R.layout.item_news, parent, false);
    return new ViewHolder(mView);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
   final DtoNews news = mData.get(position);

    holder.mMessageTitle.setText(news.getmUserName());

    if(!news.getmImageUrl().equals("null"))
        setImageNews(holder.mImageNews, news.getmImageUrl());
    else
        ((RelativeLayout) mView.findViewById(R.id.relative_image)).setVisibility(GONE);

    holder.mTextLike.setText("" + news.getmLikes());
    holder.mLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    holder.mMessageDescription.setText(news.getmDescription());
}

@Override
public int getItemCount() {
    return mData.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public final View mView;
    public final TextViewSansPro mMessageTitle;
    public final TextViewSansPro mMessageDescription;
    public final TextViewSansPro mTextLike;
    public final ImageView mImageNews;
    public final ImageView mLike;

    public ViewHolder(View view) {
        super(view);
        mView = view;
        mMessageTitle = (TextViewSansPro) view.findViewById(R.id.publisher_name);
        mMessageDescription = (TextViewSansPro) view.findViewById(R.id.text_noticia);
        mTextLike = (TextViewSansPro) view.findViewById(R.id.text_like);
        mImageNews = (ImageView) view.findViewById(R.id.image_news);
        mLike = (ImageView) view.findViewById(R.id.like_icon);
    }

    @Override
    public String toString() {
        return super.toString() + " '" + mMessageDescription.getText() + "'";
    }
}

public void setData(ArrayList<DtoNews> data, boolean isRefresh){
    if(isRefresh) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }
    else{
        mData.addAll(data);
        notifyItemRangeInserted(mData.size() - 1, mData.size() - 1 + data.size());
    }
}

private void setImageNews(final ImageView view, String url){
    NetworkCacheSingleton.getImageLoader().get(url, new ImageLoader.ImageListener() {
        @Override
        public void onResponse(final ImageLoader.ImageContainer response, boolean isImmediate) {
                view.setImageBitmap(response.getBitmap());
        }
        @Override
        public void onErrorResponse(VolleyError error) {
            ((RelativeLayout) mView.findViewById(R.id.relative_image)).setVisibility(GONE);
        }
    });
}

public ArrayList<DtoNews> getmData() {
    return mData;
}

}

A little more information, this list works with paging: For the 1st time the service returns the 1st 5 items that I am going to show and I call public void setData(ArrayList<DtoNews> data, boolean isRefresh) to save them in ArrayList<DtoNews> . Once in the list scrolling down I call the same service which returns me the following 5 that I am entering within the ArrayList following the data that were already. So on until you get more data and have the complete list. I clarify that the data come to me correctly in the established order.

    
asked by Jorge Gonzalez 09.03.2017 в 00:26
source

1 answer

1

Try to use the setImageBitmap :

holder.item_picture.setImageBitmap(item.getPhoto_bitmap());

I share my class:

package texium.mx.drones.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import texium.mx.drones.R;
import texium.mx.drones.fragments.PhotoGalleryFragment;
import texium.mx.drones.models.DecodeGallery;
import texium.mx.drones.models.TaskGallery;
import texium.mx.drones.utils.Constants;

/**
 * Created by saurett on 14/01/2016.
 */
public class PhotoGalleryAdapter extends
    RecyclerView.Adapter<PhotoGalleryAdapter.ViewHolder>{

    View.OnClickListener onClickListener;
    List<TaskGallery> photo_gallery_list = new ArrayList<>();

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView item_picture;
        Button sync_button;
        Button description_button;
        Button delete_button;

        public ViewHolder(View itemView) {
            super(itemView);

            item_picture = (ImageView) itemView.findViewById(R.id.item_photo);
            sync_button = (Button) itemView.findViewById(R.id.item_photo_sync);
            description_button =
                (Button) itemView.findViewById(R.id.item_photo_description);
            delete_button = (Button) itemView.findViewById(R.id.item_photo_delete);
        }
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
    public TaskGallery getItemByPosition(int position)
    {
        return photo_gallery_list.get(position);
    }
    public void addAll(List<TaskGallery> photos_list)
    {
        this.photo_gallery_list.addAll(photos_list);
    }
    public void remove(int position) { this.photo_gallery_list.remove(position);}

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =
            LayoutInflater
            .from(parent.getContext())
            .inflate(R.layout.item_picture_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final TaskGallery item = photo_gallery_list.get(position);

        int syncType = item.getSync_type().intValue();

        int resource;
        switch (syncType) {
            case Constants.ITEM_SYNC_SERVER_DEFAULT:
                resource = R.mipmap.ic_computer_black;
                break;
            case Constants.ITEM_SYNC_LOCAL_TABLET:
                resource = R.mipmap.ic_tablet_android_black;
                break;
            case Constants.ITEM_SYNC_SERVER_CLOUD:
                resource = R.mipmap.ic_cloud_black;
                break;
            case Constants.ITEM_SYNC_SERVER_CLOUD_OFF:
                resource = R.mipmap.ic_cloud_off_black;
                break;
            default:
                resource = android.R.drawable.ic_menu_delete;
                break;

        }

        final DecodeGallery decodeGallery = new DecodeGallery();

        decodeGallery.setTaskGallery(item);

        holder.sync_button.setBackgroundResource(resource);
        holder.item_picture.setImageBitmap(item.getPhoto_bitmap());

        holder.sync_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                decodeGallery.setIdView(v.getId());
                decodeGallery.setPosition(position);
                PhotoGalleryFragment.showQuestion(decodeGallery);
            }
        });

        holder.delete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                decodeGallery.setIdView(v.getId());
                decodeGallery.setPosition(position);
                PhotoGalleryFragment.showQuestion(decodeGallery);
            }
        });

        holder.description_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                decodeGallery.setIdView(v.getId());
                decodeGallery.setPosition(position);
                PhotoGalleryFragment.showQuestion(decodeGallery);
            }
        });
    }

    @Override
    public int getItemCount() {
        return photo_gallery_list == null ? 0 : photo_gallery_list.size();
    }

    public void removeItem(int position) {
        this.photo_gallery_list.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, getItemCount() - position);
    }
}
    
answered by 14.03.2017 в 22:22