14

我有一个 ListView 显示的东西,其中许多有说明性的图像,其中一些没有。ViewHolder 使用 NetworkImageView 来显示插图。

问题是即使设置了默认图像,它也不会显示,直到调用 setImageUrl(),如果 url 为空,它又会将视图的源位图设置为 null 。如果 url 不为空,它会发出请求。这意味着即使没有与该特定视图关联的有效网络 url,也必须强制发出网络请求,否则将显示一个空视图,而不是显示默认图像。

(问题提交:https ://code.google.com/p/android/issues/detail?id=59538 )

有没有办法对没有有效 url 的项目发出虚假的网络请求?

4

4 回答 4

39

这对我来说很好:

    String url = getURL();
    NetworkImageView niv = (NetworkImageView)rootView.findViewById(R.id.niv);
    if(url.length() > 0)
        niv.setImageUrl(url, imageLoader);
    niv.setDefaultImageResId(R.drawable._default);
    niv.setErrorImageResId(R.drawable.error);
于 2014-02-08T10:01:53.710 回答
1

对于 NetworkImageView 定义了一个名为的变量,mDefaultImageId 您可以使用它来定义默认图像的资源

这是你可以做到的 -

创建一个名为 的文件attrs.xml,在其中放置以下行 -

<declare-styleable name="DefaultImageResId">
    <attr name="default_image_resource" format="reference" />
</declare-styleable>

创建一个名为CustomNetworkImageView并使用以下代码的类 -

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.android.volley.toolbox.NetworkImageView;
import com.myapp.R;

public class CustomNetworkImageView extends NetworkImageView {
    public CustomNetworkImageView(Context context) {
        super(context);
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDefaultImageResId(context, attrs);
    }

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

    private void setDefaultImageResId(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DefaultImageResId);
        int resId = typedArray.getResourceId(R.styleable.DefaultImageResId_default_image_resource, 0);

        if (0 != resId)
            setDefaultImageResId(resId);
    }
}

现在在您的常规布局 xml 文件中像这样使用 -

<com.myapp.CustomNetworkImageView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/img"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:default_image_resource="@drawable/defaultimage"/>

请记住,行在xmlns:app="http://schemas.android.com/apk/res-auto"布局文件的根元素中非常重要然后添加app:default_image_resource

你完了!!!现在您甚至可以从您的 xml 中提及图像

于 2015-10-17T08:13:49.027 回答
0

也许你可以试试 android:background="yourdrawable"

<com.android.volley.toolbox.NetworkImageView
                    android:id="@+id/puppy_imageView"
                    android:layout_width="110dp"
                    android:layout_height="110dp"
                    android:scaleType="centerCrop"
                    android:background="@drawable/drawablename"

                    />

更新答案

在我尝试这个答案之后,我认为新图像将覆盖背景,所以它不是一个好的答案

根据文档setDefaultImageResId可以做到这一点

设置用于此视图的默认图像资源 ID,直到尝试加载它完成。

只是当你定义你的

NetworkImageView _ImageView = (NetworkImageView) findViewById(R.id.imageview);
_ImageView.setDefaultImageResId(R.drawable.yourdrawable);
于 2015-11-29T19:36:53.507 回答
0

ImageView是一个超类,所以使用它自己的 setter 方法开始:

niv.setImageResource(R.drawable.icon_no_image_avail);   // coerce NetworkImageView to draw itself initially.
于 2016-06-15T17:19:10.960 回答