[英]Android Glide Image is not showing on the first screen
我正在使用Android glide加載遠程圖像,但是在這里我遇到了一個奇怪的問題,第一個屏幕上的圖像沒有顯示,但是當我向下滾動並再次向上滾動時,這些圖像變得正常顯示。以下是屏幕截圖: 我也用谷歌搜索了這個問題並找到了一個重復的文件 ,在嘗試了這些步驟后它仍然不起作用,您可能需要檢查代碼,就是這樣:
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;
}
}
如果以前有人遇到過這樣的場面,請先謝謝。
參見https://github.com/bumptech/glide/blob/master/README.md#compatibility
圓形圖片 :
CircleImageView
/CircularImageView
/RoundedImageView
已知有問題與TransitionDrawable
(.crossFade()
與.thumbnail()
或.placeholder()
和GIF動畫,使用BitmapTransformation
(.circleCrop()
將在V4使用)或.dontAnimate()
解決此問題。
簡而言之:舍入視圖總是將傳入的Drawable柵格化為位圖,如果Drawable內有動畫(GIF或crossFade),則將不起作用。
在第一次加載時,在檢索圖像時顯示占位符,然后交叉淡入淡出到圖像; 稍后,當您向上滾動時,圖像會立即從內存緩存中加載,因此沒有動畫。
在https://github.com/bumptech/glide/issues/1508響應時,我發現圖像未加載的潛在根本原因:在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!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.