我有一個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 –