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);
}
}
我使用毕加索库将网址更改为图片。当我在列表中选择smth时,其他项目上的图像会随机更改(始终使用其他项目中的现有网址进行更改)
编辑:我试图使用Universal-Image-Loader,但我一无所获。它可能是实施问题。答案 0 :(得分:1)
试试这个:
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();
}
}
应用程序启动时,需要编写一次包含//-----------This lines---------------//
注释的代码。如果您有任何ApplicationLoader
类,那么请将其添加到其中。每次适配器初始化时都不需要写这些行。!!
或者将它们保留原样不会有任何区别。这只是一个澄清。