關於Widget的介紹和用法能夠看這兩篇博文,寫的很是詳細了html
Android 之窗口小部件詳解--App Widget http://www.cnblogs.com/skywang12345/p/3158310.html 網絡
Android 之窗口小部件高級篇--App Widget 之 RemoteViews http://www.cnblogs.com/skywang12345/p/3264991.htmlide
上面兩篇博文沒有提到怎麼給Widget中的ListView或者GridView中顯示網絡圖片,而咱們的項目的需求中須要Widget中的ListView填充接口得到的文字和圖片,試過不少方法最後發現仍是如下這種僞同步比較讓人滿意。oop
private Map<Integer, Boolean> flags = Collections.synchronizedMap(new HashMap<Integer, Boolean>()); private Bitmap mBitmap; private Handler handler = new Handler(Looper.getMainLooper()); @Override public RemoteViews getViewAt(final int position) { //這裏初始化RemoteView,設置監聽,等等.... flags.put(position, false); handler.post(new Runnable() { @Override public void run() { //這裏用了ImageLoader,或者別的帶有加載回調加載庫 ImageLoader.getInstance().loadImage(datas.get(position).getImgUrl, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { flags.put(position, true); } @Override public void onLoadingCancelled(String imageUri, View view) { flags.put(position, true); } @Override public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) { mBitmap = bitmap; flags.put(position, true); } }); } }); //在此一直等待獲取到網絡圖片 while (!flags.get(position)) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } flags.put(position, false); if (mBitmap != null) { mRemoteViews.setImageViewBitmap(R.id.widget_item_image, mBitmap); } else { mRemoteViews.setImageViewResource(R.id.widget_item_image, R.mipmap.ic_launcher); } mBitmap = null; return mRemoteViews; }