Android,Glide显示错误的图像大约一秒钟 [英] Android, Glide shows wrong image for about one second

查看:222
本文介绍了Android,Glide显示错误的图像大约一秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Glide库从URL加载图像,而URL是从Graph Request(Facebook)获得的.在RecyclerAdapter中使用.当我滚动时,每个ImageView会在不到一秒钟的时间内显示错误的图片,然后进行纠正.这是代码:

I'm using Glide library for loading images from URL, which I get from Graph Request (Facebook). It's used in RecyclerAdapter. When I'm scrolling, each ImageView shows wrong picture for about less than one second, then correct one. Here is the code:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

    ImageView main_image = viewHolder.main_image,

    getEventCover(position, main_image);

        }

private void getEventCover(int position, final ImageView img){

        GraphRequest request = GraphRequest.newGraphPathRequest(
                AccessToken.getCurrentAccessToken(),
                event.get(position).getEventID(),
                new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(GraphResponse response) {

                        JSONObject data = response.getJSONObject();

                        try {
                            JSONObject cover = data.getJSONObject("cover");
                            String source = cover.getString("source");

                            Glide.with(fragment)
                                    .load(source)
                                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                                    .centerCrop()
                                    .into(img);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );

        Bundle parameters = new Bundle();
        parameters.putString("fields", "cover");
        request.setParameters(parameters);
        request.executeAsync();
}

推荐答案

您的问题"是,您使用的ImageView从先前的行中回收(滚动时消失).因此,您在onBindViewHolder中的ImageView已经包含先前的图像.

Your "problem" is that the ImageViews you are using are recycled from previous rows (which are disappearing while scrolling). Thus your ImageView in onBindViewHolder already contains a previous image.

此图像一直显示到对新图像的网络请求(GraphRequest和Glide)完成为止.

This image is displayed until the network requests (GraphRequest and Glide) for the new image are finished.

为避免此问题,您必须在调用getEventCover之前清除ImageView.可以通过设置占位符图像或通过设置透明图像来完成此操作,如下所示:

To avoid the problem you have to clear your ImageView before calling getEventCover. This can be done by setting an placeholder image or by setting a transparent image as follows:

main_image.setImageResource(android.R.color.transparent);

这篇关于Android,Glide显示错误的图像大约一秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆