我使用毕加索在recyclerview中显示图像,但在加载图像完成时它不会自动刷新项目,我必须手动滚动才能显示图像。我有什么办法来修复它,使recyclerview项目在完成加载后自动更新显示?
这是我的适配器类:
public class LocationListRecyclerViewAdapter extends RecyclerView
.Adapter<LocationListRecyclerViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "LocationListRecyclerViewAdapter";
private ArrayList<LocationObject> mDataset;
private static MyClickListener myClickListener;
private Context mContext;
static String imageUrls;
public static String[] thumbnailUrl;
private LayoutInflater inflater;
public LocationListRecyclerViewAdapter(Context context, ArrayList<LocationObject> mDataset) {
this.mContext = context;
this.mDataset = mDataset;
}
public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView locName;
TextView locAddress;
TextView locDistance;
RoundedImageView roundedImageView;
public DataObjectHolder(View itemView) {
super(itemView);
this.locName = (TextView) itemView.findViewById(R.id.location_name);
this.locAddress = (TextView) itemView.findViewById(R.id.location_address);
this.locDistance = (TextView) itemView.findViewById(R.id.location_distance);
this.roundedImageView = (RoundedImageView) itemView.findViewById(R.id.thumbnail_location);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public LocationListRecyclerViewAdapter(ArrayList<LocationObject> myDataset) {
mDataset = myDataset;
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.location_listcard_item, parent, false);
this.mContext = parent.getContext();
thumbnailUrl = new String[10];
TextView locName = (TextView) view.findViewById(R.id.location_name);
TextView locAddress = (TextView) view.findViewById(R.id.location_address);
TextView locDistance = (TextView) view.findViewById(R.id.location_distance);
Typeface helvetica = Typeface.createFromAsset(parent.getContext().getApplicationContext().getAssets(),"fonts/Helvetica.otf");
locName.setTypeface(helvetica);
locAddress.setTypeface(helvetica);
locDistance.setTypeface(helvetica);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
> @Override
> public void onBindViewHolder(DataObjectHolder holder, int position) {
LocationObject item = mDataset.get(position);
holder.locName.setText(mDataset.get(position).getLocationName());
holder.locAddress.setText(mDataset.get(position).getLocationAddress());
holder.locDistance.setText(String.valueOf(mDataset.get(position).getLocationDistance())+"m");
Log.i(position+" photos"," = "+mDataset.get(position).getLocationPhoto());
//mContext
Picasso.with(holder.roundedImageView.getContext()).cancelRequest(holder.roundedImageView);
Picasso.with(holder.roundedImageView.getContext())
.load(mDataset.get(position).getLocationPhoto())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.resize(200,200)
.into(holder.roundedImageView);
holder.itemView.setTag(item);
}
public void addItem(LocationObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
@Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
答案 0 :(得分:1)
让Picasso完成它的工作,你不需要干涉下载过程,图书馆本身知道何时取消请求。
在使用图书馆之前一定要有所帮助,仔细探索它的所有优点。 http://square.github.io/picasso/
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
LocationObject item = mDataset.get(position);
holder.locName.setText(item.getLocationName());
holder.locAddress.setText(item.getLocationAddress());
holder.locDistance.setText(String.valueOf(item.getLocationDistance())+" m");
Picasso.with(mContext)
.load(item.getLocationPhoto())
.placeholder(R.drawable.placeholder)
.resize(200,200)
.centerCrop()
.into(holder.roundedImageView);
}