带有Glide图像的RecyclerView加载,但在我向后滚动后,它们正在消失

时间:2016-11-29 08:57:39

标签: android image android-recyclerview android-glide

我有一个RecyclerView,每个RecyclerView项目都有ImageView。我正在使用Glide在ImageView中加载图像,当我向下滚动RecyclerView时它会加载图像,这很好,但是在我向后滚动后它们会消失。此处还附有适配器的整个代码

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.DataObjectViewHolder> {
private Context context;
private List<DataObject> data;
OnItemClickListener mItemClickListener;

public class DataObjectViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public CardView cv;
    public TextView title;
    public ImageView image;
    public Button thumb;
    public TextView thumbtext;
    public TextView creadetAt;
    public TextView author;

    public DataObjectViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);
        title = (TextView) itemView.findViewById(R.id.title);
        image = (ImageView) itemView.findViewById(R.id.image);
        thumb = (Button) itemView.findViewById(R.id.thumbimage);
        thumbtext = (TextView) itemView.findViewById(R.id.thumbtext);
        creadetAt = (TextView) itemView.findViewById(R.id.date);
        author = (TextView) itemView.findViewById(R.id.author);

        thumb.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick(itemView, getPosition(), thumbtext);
        }
    }
}

public interface OnItemClickListener {
    void onItemClick(View view, int position, TextView thumbtext);
}

public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
}

RecyclerViewAdapter(Context context, List<DataObject> data) {
    this.context = context;
    this.data = data;
}

@Override
public DataObjectViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_adapter, viewGroup, false);
    DataObjectViewHolder doh = new DataObjectViewHolder(v);
    return doh;
}
// check if refresh data or load more
public void addNewItem(List<DataObject> dataObjects) {
    if (MainActivity.refresh){
        data.addAll(0,dataObjects);
    } else {
        data.addAll(dataObjects);
    }
}
public void remove(int position){
    data.remove(position);
    notifyItemRemoved(position);
}

@Override
public void onBindViewHolder(final DataObjectViewHolder dataObjectViewHolder, int position) {
    if (data.get(position).getUpload_image() != null){
        String uri = data.get(position).getUpload_image();
        Glide.with(context)
                .load(uri)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .fitCenter()
                .error( R.drawable.error )
                .placeholder(R.drawable.ic_close_white_48dp)
                .into(dataObjectViewHolder.image);
    }else {
        Glide.clear(dataObjectViewHolder.image);
    }

    CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(Long.parseLong("1477002851930"),System.currentTimeMillis(),DateUtils.SECOND_IN_MILLIS);
    dataObjectViewHolder.title.setText(data.get(position).getTitle());
    dataObjectViewHolder.thumbtext.setText(data.get(position).getId());
    dataObjectViewHolder.creadetAt.setText(timeAgo);
    dataObjectViewHolder.author.setText("by " + data.get(position).getAuthor());
}

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

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

这是正确的适配器文件

public class RecyclerAdapter extends    RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
List<DataObject> data;
Context context;
View v;
boolean isExist;
private onClick mOnClick;

RecyclerAdapter(Context context, List<DataObject> data){
    this.context = context;
    this.data = data;
}

public void setOnClick(final onClick mOnClick) {
    this.mOnClick = mOnClick;
}

public interface onClick{
    void onButtonClicked(View view, ToggleButton mThumbButton, int position, long id);
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public ImageView mImageView;
    public ToggleButton mThumbButton;
    public TextView mThumbText;
    public TextView mTitleImage;
    public TextView mDateText;

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

        mImageView = (ImageView) itemView.findViewById(R.id.imageView);
        mThumbButton = (ToggleButton) itemView.findViewById(R.id.thumbButton);
        mThumbText = (TextView) itemView.findViewById(R.id.thumbText);
        mTitleImage = (TextView) itemView.findViewById(R.id.titleImage);
        mDateText = (TextView) itemView.findViewById(R.id.dateText);
        this.setIsRecyclable(false);
        mThumbButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (mOnClick != null){
            mOnClick.onButtonClicked(itemView, mThumbButton, getAdapterPosition(),data.get(getAdapterPosition()).getId());
        }
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int ViewType) {
    v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_adapter, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;

}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    if (data.get(position).getUpload_image() != null){
        String uri = data.get(position).getUpload_image();
        Glide.with(context)
                .load(uri)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                //.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .error( R.drawable.error )
                .placeholder(R.drawable.ic_close_white_48dp)
                .into(holder.mImageView);
    }else {
        Glide.clear(holder.mImageView);
    }
    CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(Long.parseLong(String.valueOf(data.get(position).getDate_add())),System.currentTimeMillis(),DateUtils.SECOND_IN_MILLIS);
    holder.mTitleImage.setText(String.valueOf(data.get(position).getTitle()));
    holder.mThumbText.setText(String.valueOf(data.get(position).getId()));
    holder.mDateText.setText(timeAgo);
    int i = MainActivity.sharedPref.getInt(MainActivity.prefName+data.get(position).getId(), 2);
    if (i == 1) {
        holder.mThumbButton.setChecked(true);
    }else
    {
        holder.mThumbButton.setChecked(false);
    }
}

public void addNewItem(List<DataObject> dataObjects) {
    if (MainActivity.refresh){
        for(int i=0; i < dataObjects.size(); i++ ){
           isExist = data.contains(dataObjects.get(i));
            if (!isExist){
                data.add(i,dataObjects.get(i));
                notifyDataSetChanged();
            }
            //Log.d("isExist", " isExist " + data.contains(dataObjects.get(i)) + " id " + dataObjects.get(i).getId());
        }
    } else {
        data.addAll(dataObjects);
        notifyDataSetChanged();
    }
    //Log.d("isExist", " data size " + data.size() + " data object size " + dataObjects.size());
}

public void remove(int position){
    data.remove(position);
    notifyItemRemoved(position);
}

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

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

0 个答案:

没有答案