【问题标题】:Android Volley ImageListener onResponse behaviourAndroid Volley ImageListener onResponse 行为
【发布时间】:2014-03-11 22:16:52
【问题描述】:

我正在使用 Android Volley 库,如果您知道的话,我的问题应该很容易回答;)。

我需要知道com.android.volley.toolbox.ImageLoader.ImageListener 在 处理成功的响应。文档说

The call flow is this: 1. Upon being attached to a request, 
onResponse(response, true) will be invoked to reflect any cached data 
that was already available. If the data was available, response.getBitmap() will 
be non-null. 2. After a network response returns, only one of the following 
cases will happen: - onResponse(response, false) will be called if the 
image was loaded. or - onErrorResponse will be called if there was an error 
loading the image.

我想知道的是:这是否意味着我可以将onResponse 调用两次(首先将isImmediate 设置为false,然后设置为true)?我可以依靠吗?我的意思是会一直这样(如果图片加载成功的话)?

我正在尝试做这样的事情

imageLoader.get(image.getUrl(), new ImageListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.call(null, error);
                    }

                    @Override
                    public void onResponse(ImageContainer response,
                            boolean isImmediate) {
                        if (response.getBitmap() != null) {
                            callback.call(response.getBitmap(), null);
                        }
                    }
                });

当图像可以成功加载时,我需要调用callback.call(),并且我还需要response.getBitmap() 来返回实际位图而不是null

提前致谢!

【问题讨论】:

    标签: android caching request response android-volley


    【解决方案1】:

    我想知道的是:这是否意味着我可以调用两次 onResponse(首先将 isImmediate 设置为 false,然后设置为 true)?我可以依靠吗?我的意思是会一直这样(如果图片加载成功的话)?

    简短回答:

    除非这个类的实现被改变(我非常怀疑这会发生),否则你可以依赖这个调用顺序被尊重。

    如果有帮助,我在此处的另一个答案中详细介绍了三种可能的响应案例:https://stackoverflow.com/a/32464875/3227795

    【讨论】:

      猜你喜欢
      • 2020-01-27
      • 2021-01-05
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2021-07-31
      相关资源
      最近更新 更多