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

时间:2016-10-09 03:31:44

标签: android android-glide

我使用Android滑动来加载远程图像,但在这里我遇到了一个奇怪的问题,第一个屏幕上的图像没有显示但是当我向下滚动并再次向上滚动时,这些图像正常显示。屏幕截图是: enter image description here 我也搜索了这样的问题并找到了duplicate,在尝试了这些步骤之后它仍然无法正常工作,您可能需要检查代码,就在这里:

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;
    }
}

如果之前有人见过这样的场景,请提前谢谢。

2 个答案:

答案 0 :(得分:2)

请参阅https://github.com/bumptech/glide/blob/master/README.md#compatibility

  

圆形图片CircleImageView / CircularImageView / RoundedImageView已知issues TransitionDrawable .crossFade()使用.thumbnail().placeholder())和动画GIF,使用BitmapTransformation(v4中将提供.circleCrop())或.dontAnimate()来解决问题。

简而言之:舍入视图总是将传入的Drawables光栅化为位图,如果Drawable(GIF或crossFade)中有动画,则无法使用。

在第一次加载时,在检索图像时显示占位符,然后交叉淡化到图像;之后,当您向上滚动时,图像会立即从内存缓存中加载,因此没有动画。

答案 1 :(得分:1)

intval回复时,我发现图片无法加载的潜在根本原因:在getView中,您覆盖siteHolder,因此无论ImageTask完成,它都会更新最后绑定的行而不是启动任务的原始行。有关如何在这样的双分派案例中改进Glide相关代码的更多提示,请参阅GitHub上的问题。快速而肮脏的修复:

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!
}