简体   繁体   English

Android Glide Image没有显示在第一个屏幕上

[英]Android Glide Image is not showing on the first screen

I am using Android glide to load remote images, but here i met a strange problem which image on the first screen not showing but while i scroll down and scroll up again, those images become showing normally.here is the screen shot: 我正在使用Android glide加载远程图像,但是在这里我遇到了一个奇怪的问题,第一个屏幕上的图像没有显示,但是当我向下滚动并再次向上滚动时,这些图像变得正常显示。以下是屏幕截图: 在此处输入图片说明 I have also googled such question and find a duplicate ,and after trying such steps it still doesn't work, and you may need to check code, here is it: 我也用谷歌搜索了这个问题并找到了一个重复的文件 ,在尝试了这些步骤后它仍然不起作用,您可能需要检查代码,就是这样:

public class SiteAdapter extends ArrayAdapter<Site> {
private int resourceId;
private List<Site> sites = null;
private Context context;
private SiteHolder siteHolder;
/**
 * @param context the current activity context, we can get it using the super ArrayAdapter constructor
 * @param resource the site_layout.xml file
 * @param objects the collection to store all the sites
 */
public SiteAdapter(Context context, int resource, List<Site> objects) {
    super(context, resource, objects);
    this.context = context;
    this.resourceId = resource;
    this.sites = objects;
}

@Override
public Site getItem(int position) {
    return sites.get(position);
}

@Override
public int getCount() {
    return sites.size();
}

//get the viewpage which inflate by site_layout.xml file
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Site site = getItem(position);
    View view = convertView;
    siteHolder = new SiteHolder();
    if (view == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(resourceId, null);
    }
    //this place we need to get the whole widget in site_layout.xml file
    siteHolder.image = (ImageView) view.findViewById(R.id.thumbnail);
    siteHolder.address = (TextView)view.findViewById(R.id.address);
    siteHolder.mallName = (TextView) view.findViewById(R.id.name);
    siteHolder.distance = (TextView) view.findViewById(R.id.distance);
    siteHolder.address.setText(site.getAddress());
    //set name of the view
    siteHolder.mallName.setText(site.getName());
    //set price of the view
    //set distance of the view
    siteHolder.distance.setText("<" + site.getDistance() + "m");
    //set image
    ImageTask task = new ImageTask();
    task.execute("http://xxxx/springmvc/getFirst/" + site.getName());
    return view;
}

//检测adapater中加载网络资源是否可行?结论:不可行
public String getImgUrl(String str){
    String data = "";
    try {
        URL u = new URL(str);
        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
        InputStream is = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String content = "";
        StringBuffer sb = new StringBuffer();
        while((content = br.readLine()) != null){
            sb.append(content);
        }
        data = sb.toString();
        br.close();
        is.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return data;
}
class ImageTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... str) {
        String data = "";
        data = getImgUrl(str[0]);
        return data;
    }

    @Override
    protected void onPostExecute(String s) {
        Glide.with(context)
            .load(s)
            .fitCenter()
            .into(siteHolder.image);
    }
}

class SiteHolder{
    ImageView image;
    TextView mallName;
    TextView address;
    TextView distance;
    }
}

if there anybody who met such scene before, thank you in advance. 如果以前有人遇到过这样的场面,请先谢谢。

See https://github.com/bumptech/glide/blob/master/README.md#compatibility 参见https://github.com/bumptech/glide/blob/master/README.md#compatibility

Round Pictures : CircleImageView / CircularImageView / RoundedImageView are known to have issues with TransitionDrawable ( .crossFade() with .thumbnail() or .placeholder() ) and animated GIFs, use a BitmapTransformation ( .circleCrop() will be available in v4) or .dontAnimate() to fix the issue. 圆形图片CircleImageView / CircularImageView / RoundedImageView已知有问题TransitionDrawable.crossFade().thumbnail().placeholder()和GIF动画,使用BitmapTransformation.circleCrop()将在V4使用)或.dontAnimate()解决此问题。

In short: the rounding views always rasterize the incoming Drawables into Bitmaps, which won't work if there's an animation inside the Drawable (GIF or crossFade). 简而言之:舍入视图总是将传入的Drawable栅格化为位图,如果Drawable内有动画(GIF或crossFade),则将不起作用。

On the first load the placeholder is shown while retrieving the image and then crossFaded to the image; 在第一次加载时,在检索图像时显示占位符,然后交叉淡入淡出到图像; later, when you scroll up the image is loaded from memory cache immediately so there's no animation. 稍后,当您向上滚动时,图像会立即从内存缓存中加载,因此没有动画。

While responding at https://github.com/bumptech/glide/issues/1508 I discovered the potential root cause of your images not loading: in getView you overwrite siteHolder , so whichever ImageTask finishes, it always updates the last bound row instead of the original row that the task was started for. https://github.com/bumptech/glide/issues/1508响应时,我发现图像未加载的潜在根本原因:在getView您覆盖siteHolder ,所以无论ImageTask完成了什么,它总是更新最后一个绑定行,而不是更新任务开始的原始行。 For more tips on how to improve Glide related code in a double-dispatch case like this, see the issue on GitHub. 有关如何在双发情况下改进Glide相关代码的更多技巧,请参阅GitHub上的问题。 Quick and dirty fix: 快速而肮脏的修复:

public class SiteAdapter extends ArrayAdapter<Site> {
    //private SiteHolder siteHolder; // remove this field

public View getView(int position, View convertView, ViewGroup parent) {
    SiteHolder siteHolder = new SiteHolder();
    ...
    ImageTask task = new ImageTask(siteHolder);
    ...
}

static class ImageTask extends AsyncTask<String, Void, String> {
    private SiteHolder siteHolder; // TODO initialize in constructor
    // rest stays the same, but notice the `static` modifier above!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM