我有一个recyclerview的问题:当我在列表中上下移动时,一些行的图像被交换(或者通常用其他行的图像设置几行)。我认为问题出在adpter上,但我不明白什么是错的。RecyclerView与位图
这myAdapter.java:
public class myAdapter extends RecyclerView.Adapter<myAdapter.MyViewHolder> {
private List<myItem> myList;
private Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView photo;
public TextView name;
public MyViewHolder(View view) {
super(view);
photo = (ImageView) view.findViewById(R.id.rowPhoto);
name = (TextView) view.findViewById(R.id.rowName);
}
}
public myAdapter(List<myItem> myList,Context context) {
this.myList = myList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_custom, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
myItem c = myList.get(position);
if(c.getPathFoto()!=null){
File f = new File(c.getPathPhoto());
Bitmap b = decodeFile(f);
holder.photo.setImageBitmap(b);
}
holder.name.setText(c.getName());
}
public Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE=70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth/scale/2 >= REQUIRED_SIZE &&
o.outHeight/scale/2 >= REQUIRED_SIZE) {
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
}
所有的图像(我使用)此前已保存在/数据/数据/ MYAPP/app_data文件/ IMAGEDIR(例如/数据/数据/ MYAPP /app_data/imageDir/21432342.png)。
我该如何解决问题?
我建议使用滑翔作为库加载图像,是简单了很多,而速度更快。 https://github.com/bumptech/glide –