适配器不会在Recycler View

时间:2016-02-15 06:47:47

标签: android android-recyclerview

我正在使用回收站视图,在循环器视图中我有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);
        }
    }

这是位图截图,如下所示,它不是空的。

enter image description here

2 个答案:

答案 0 :(得分:4)

在GitHub上的

Here我把代码放在实现的整个流程中。

主要问题是使用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);
        }
    }

}

答案 1 :(得分:0)

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

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

并且在适配器中,

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

它将完成你的所有工作。