android - 带位图的 RecyclerView

标签 android bitmap android-adapter android-recyclerview android-bitmap

我在使用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;
    }

}

所有图像(我使用的)之前都已保存在/data/data/myapp/app_data/imageDir 中(例如/data/data/myapp/app_data/imageDir/21432342.png)。

如何解决这个问题?

最佳答案

我认为当图像为空时您没有清除图像。

尝试将 OnBind 方法更改为:

@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);
    } else {
        holder.photo.setImageBitmap(null);
    }

    holder.name.setText(c.getName());
}

关于android - 带位图的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35523659/

相关文章:

java - 几个小时后前台服务停止

java - 消息、处理程序和线程 : Lego Mindstorms bluetooth communication

android - 在不创建位图的情况下获取 Drawable 的 byte[]

Android GridView 根据数据更改列数

android - GridView 无法显示来自 Adapter 的元素

android - 微调器 : get state or get notified when opens

java - 无法在设备上安装应用程序。 (使用 map API)

android - android中圆的尖角(在 Canvas 上绘制)

java - 人脸检测并使用边界框提取人脸并创建新的位图

android - ImageView 在两次点击后更改图像