【发布时间】: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