问题
private DisplayImageOptions options;
@Override
public TaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.task, parent, false);
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.postProcessor(new BitmapProcessor() {
@Override
public Bitmap process(Bitmap bitmap) {
return Bitmap.createScaledBitmap(bitmap,200,200,false);
}
})
.build();
TaskViewHolder tvh = new TaskViewHolder(v);
return tvh;
}
@Override
public void onBindViewHolder(TaskViewHolder holder, int position) {
holder.task_title.setText(tasks.get(position).get_title());
holder.task_date_to_end.setText(tasks.get(position).get_description());
try {
/*Picasso.with(context).load(tasks.get(position).get_url_to_icon()).resize(200, 200).into(holder.task_url_to_icon);*/
ImageLoader.getInstance().displayImage(tasks.get(position).get_url_to_icon(),holder.task_url_to_icon,options);
} catch (Exception e) {}
if (tasks.get(position).isSelected()) {
holder.task_layout.setBackgroundColor(Color.parseColor("#d5d5d5"));
} else {
holder.task_layout.setBackgroundColor(Color.TRANSPARENT);
}
}
Im using picasso library to change url to image. While i selecting smth on list the image on the other items changes randomly (always changing with existing urls from another items)
EDIT: Im tried to use Universal-Image-Loader, but i,ve got nothing. Its probably implementation problem.
回答1:
Try this :
public class TaskAdapter extends RecyclerView.Adapter<TaskViewHolder> {
List<Task> tasks;
protected Context context;
private DisplayImageOptions options;
public TaskAdapter(List<Task> tasks, Context context) {
//-----------This lines---------------//
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache()).diskCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
//-----------This lines---------------//
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.face)
.showImageForEmptyUri(R.drawable.face)
.showImageOnFail(R.drawable.face).cacheInMemory(true)
.cacheOnDisk(true).considerExifParams(true).build();
this.tasks = tasks;
this.context = context;
}
@Override
public TaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.task, parent, false);
TaskViewHolder tvh = new TaskViewHolder(v);
return tvh;
}
@Override
public void onBindViewHolder(TaskViewHolder holder, int position) {
holder.task_title.setText(tasks.get(position).get_title());
holder.task_date_to_end.setText(tasks.get(position).get_description());
holder.task_url_to_icon.setVisibility(View.VISIBLE);
try {
ImageLoader.getInstance().displayImage(tasks.get(position).get_url_to_icon(),holder.task_url_to_icon,options);
} catch (Exception e) {
Log.d("EXP",e.getMessage());
}
if (tasks.get(position).isSelected()) {
holder.task_layout.setBackgroundColor(Color.parseColor("#d5d5d5"));
} else {
holder.task_layout.setBackgroundColor(Color.TRANSPARENT);
}
}
public void setSelected(int pos) {
try {
tasks.get(pos).setSelected(!tasks.get(pos).isSelected());
notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
public void add(int position, Task task) {
tasks.add(task);
notifyItemInserted(position);
}
public void deleteSelectedItems() {
for(int i=0;i<getItemCount();++i){
if(tasks.get(i).isSelected()==true)
{ tasks.remove(i);
notifyItemRemoved(i);i=0;
}
}
}
@Override
public int getItemCount() {
return tasks.size();
}
}
The code surrounded with //-----------This lines---------------//
comment needed to be written once when application gets started. If you have any ApplicationLoader
class then put this in that. No need to write these line every time when adapter initializes..!!
Or leaving them as they are will not make any difference. It was just a clarification.
来源:https://stackoverflow.com/questions/37459058/image-url-changing-while-cliking-onrecyclerview-item