Android 适配器不更新回收器视图中的网络映像
我正在使用recycler视图,在recycler视图中我有networkImage。我正试图按如下方式从gallery加载图像,但它没有加载,它仍然使用默认图像。我已经测试并验证了url是否有效。我想知道我错过了什么。最初,我已经加载了一个图像,现在我正在尝试更新它 ItemData类Android 适配器不更新回收器视图中的网络映像,android,android-recyclerview,Android,Android Recyclerview,我正在使用recycler视图,在recycler视图中我有networkImage。我正试图按如下方式从gallery加载图像,但它没有加载,它仍然使用默认图像。我已经测试并验证了url是否有效。我想知道我错过了什么。最初,我已经加载了一个图像,现在我正在尝试更新它 ItemData类 public class ItemData { private String url; public ItemData(String url){ this.url = url; } String
public class ItemData {
private String url;
public ItemData(String url){
this.url = url;
}
String getUrl(){return url;}
void setUrl(String t){url = t;}
}
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);
}
}
在活动中
@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 class ItemData {
private String url;
public ItemData(String url){
this.url = url;
}
String getUrl(){return url;}
void setUrl(String t){url = t;}
}
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);
}
}
这是位图屏幕截图,如下所示,它不是空的
您应该在适配器中设置新的ArrayList。 在BaseAdapter中创建一个方法 在活动中,
listImages.add(new ItemData(path));
adapter.setNewList(your arraylist);
adapter.notifyDataSetChanged();
在适配器中
public void setNewList(Arraylist<String> newlist)
{
this.list=newlist.
}
public void setNewList(Arraylist newlist)
{
this.list=newlist。
}
它将完成您的所有工作。您应该在适配器中设置新的ArrayList。 在BaseAdapter中创建一个方法 在活动中,
listImages.add(new ItemData(path));
adapter.setNewList(your arraylist);
adapter.notifyDataSetChanged();
在适配器中
public void setNewList(Arraylist<String> newlist)
{
this.list=newlist.
}
public void setNewList(Arraylist newlist)
{
this.list=newlist。
}
它将完成您的所有工作。在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);
}
}
}
在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);
}
}
}
您正在成功地更新recyclerView中的图像,但我认为您无法以这种方式从onBindViewHolder更新ImageUtil类imageview。您应该在RecycleWebIn上使用onItemClickListner。除了另一个Commist之外,targetW和targetH在代码中肯定都是0。这是因为当你尝试使用setPic时,imgViewIcon还没有被测量。我刚刚检查过,它是150和150,它不是0。在ImageUtil类
imageView.setImageBitmap(rotatedBitmap)中调用以下行
但是在recycler视图中,我有两个项目,一个是networkimageview
,另一个是Imagebutton
。如何检测在RecyclerView
上的onItemClickListener
中单击的networkImageView
?因为当您单击networkimageview
时是添加新图像的操作,当您单击Imagebutton
时是删除单击的回收器视图的操作。看起来奇怪的大小是定义的,因为当执行onBindViewHolder
时,视图尚未测量。顺便说一句,为了处理单击,您需要您的ViewHolder实现OnClickListener
界面,并使用getAdapterPosition
方法获取项目位置。通过这种方式,您可以处理特定的项目。您正在成功地更新recyclerView中的图像,但我认为您无法通过这种方式从onBindViewHolder更新ImageUtil类imageview。您应该在RecycleWebIn上使用onItemClickListner。除了另一个Commist之外,targetW和targetH在代码中肯定都是0。这是因为当你尝试使用setPic时,imgViewIcon还没有被测量。我刚刚检查过,它是150和150,它不是0。在ImageUtil类imageView.setImageBitmap(rotatedBitmap)中调用以下行
但是在recycler视图中,我有两个项目,一个是networkimageview
,另一个是Imagebutton
。如何检测在RecyclerView
上的onItemClickListener
中单击的networkImageView
?因为当您单击networkimageview
时是添加新图像的操作,当您单击Imagebutton
时是删除单击的回收器视图的操作。看起来奇怪的大小是定义的,因为当执行onBindViewHolder
时,视图尚未测量。顺便说一句,为了处理单击,您需要您的ViewHolder实现OnClickListener
界面,并使用getAdapterPosition
方法获取项目位置。通过这种方式,您可以处理特定项目。@casillas我今天可以尝试检查复制问题。这是我们今天讨论的实际问题,@casillas我今天可以尝试检查复制问题。这是我们今天讨论的实际问题,