【问题标题】:Adapter does not update the NetworkImage in Recycler View适配器不更新回收站视图中的 NetworkImage
【发布时间】:2016-02-15 06:47:46
【问题描述】:

我正在使用回收站视图,在回收站视图中我有 networkImage。我正在尝试按如下方式从图库中加载图像,但它没有加载它,它仍然使用默认图像。我已经测试并验证了 url 是有效的。我想知道我错过了什么。最初,我加载了一张图片,现在我正在尝试更新它。

ItemData 类

public class ItemData {

private String url;

public ItemData(String url){

    this.url = url;
}

String getUrl(){return url;}
void setUrl(String t){url = t;}
}

在活动中

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
       Uri filePath = data.getData();
       Update(getRealPathFromURI(filePath));
     }
}

private void Update(String path)
{
    // update the existing view
    listImages.set(0, new ItemData(path));
    mAdapter.notifyDataSetChanged();
}

适配器

public void onBindViewHolder(ViewHolder viewHolder, int pos) {
  int position = pos;
  ImageUtil.setPic(viewHolder.imgViewIcon, itemsData.get(position).getUrl());
}

ImageUtil 类

public static void setPic(NetworkImageView imageView, String picturePath) {
        // Get the dimensions of the View
        int targetW = imageView.getWidth();
        int targetH = imageView.getHeight();

        if(targetW != 0 || targetH != 0)
        {
            // Get the dimensions of the bitmap
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = true;

            BitmapFactory.decodeFile(picturePath, bmOptions);
            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;

            // Determine how much to scale down the image
            int scaleFactor = Math.min(photoW / targetW, photoH / targetH); // zero division olasiligi...

            // Decode the image file into a Bitmap sized to fill the View
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            //bmOptions.inPurgeable = true;

            Bitmap bitmap = BitmapFactory.decodeFile(picturePath, bmOptions);

            Matrix matrix = new Matrix();
            matrix.postRotate(getImageOrientation(picturePath));
            Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);

            imageView.setImageBitmap(rotatedBitmap);
        }
    }

这是位图截图,如下图,不为空。

【问题讨论】:

  • 您正在成功更新 recyclerView 中的图像,但我认为您无法以这种方式从 onBindViewHolder 更新 ImageUtil 类图像视图。你应该在 recyclerView 上使用 onItemClickListner
  • 除了其他注释之外,targetW 和 targetH 在您的代码中肯定都是 0。这是因为当您尝试使用 setPic 时,尚未测量 imgViewIcon。
  • 我刚刚检查过,它是 150 和 150,它不是 0。在 ImageUtil 类中调用了以下行imageView.setImageBitmap(rotatedBitmap);
  • 但是在recycler视图中,我有两个项目,一个是networkimageview,另一个是Imagebutton。我如何检测到在RecyclerView 上的onItemClickListener 中单击了networkImageView?因为当你点击networkimageview 是一个添加新图片的动作,当你点击Imagebutton 是一个删除点击的回收站视图的动作。
  • 定义了看起来奇怪的大小,因为当onBindViewHolder 被执行时,视图还没有被测量。顺便说一句,要处理单击,您需要 ViewHolder 实现 OnClickListener 接口并使用 getAdapterPosition 方法获取项目位置。通过这种方式,您可以处理特定项目。

标签: android android-recyclerview


【解决方案1】:

Here 在 GitHub 上我放了实现整个流程的代码。

主要问题是使用NetworkImageView 来渲染本地图像。

您可以如下扩展NetworkImageView 并使用方法setLocalImageBitmap 以防您需要将本地图像加载到您的视图中。

public class MyImageView extends NetworkImageView {

    private Bitmap mLocalBitmap;

    private boolean mShowLocal;

    public MyImageView(Context context) {
        this(context, null);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setLocalImageBitmap(Bitmap bitmap) {
        if (bitmap != null) {
            mShowLocal = true;
        }
        this.mLocalBitmap = bitmap;
        requestLayout();
    }

    @Override
    public void setImageUrl(String url, ImageLoader imageLoader) {
        mShowLocal = false;
        super.setImageUrl(url, imageLoader);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

    super.onLayout(changed, left, top, right, bottom);
    if (mShowLocal) {
            setImageBitmap(mLocalBitmap);
        }
    }

}

【讨论】:

【解决方案2】:

您应该在适配器中设置新的 ArrayList。 在 BaseAdapter 中创建一个方法 在活动中,

listImages.add(new ItemData(path));
adapter.setNewList(your arraylist);
adapter.notifyDataSetChanged();

在适配器中,

public void setNewList(Arraylist<String> newlist)
{
this.list=newlist.
}

它会完成你所有的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2022-01-18
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多