2012-12-20 155 views
0

我有一個viewPager活動與片段。它啓動asyncTask來下載關於六個縮略圖的信息,並在postExecute上創建我的圖像類的六個對象,其中包含信息和位圖對象null。當對象被創建時,它下載圖像。構造函數:動態更改ImageView中的圖像

public MyImage(String name, String owner, String time, String date, 
     String total_rate, String votes, String description) { 
    //this.thumbnail = BitmapFactory. 
    Log.d(TAG, "Created image object"); 
    this.name = name; 
    this.owner = owner; 
    this.time = time; 
    this.date = date; 
    this.total_rate = Double.valueOf(total_rate); 
    this.votes = votes; 
    this.description = description; 

    downloadThumbnail(); 
} 

該片段有一個gridView,其中有六個縮略圖。我可以爲其設置默認圖像或progressDialog,但在下載圖像後如何更改它?設置一個監聽器?附:我對適配器瞭解不多,可能存在問題。

回答

0

您可以添加一個回調如下: 首先實現類:AsyncImageLoader 公共類AsyncImageLoader {

public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) { 
     final Handler handler = new Handler() { 
      public void handleMessage(Message message) { 
       imageCallback.imageLoaded((Drawable) message.obj, imageUrl); 
      } 
     }; 
     new Thread() { 
      @Override 
      public void run() { 
       //TODO SOMETHING 
       ...... 
      } 
     }.start(); 
     return null; 
    } 

public interface ImageCallback { 
     public void imageLoaded(Drawable imageDrawable, String imageUrl); 

    } 

然後,你可以這樣做: AsyncImageLoader asyncImageLoader =新AsyncImageLoader(); ...... 繪製對象cachedImage = asyncImageLoader.loadDrawable(IMAGEURL,新ImageCallback(){ 公共無效imageLoaded(可繪製imageDrawable,字符串IMAGEURL){

  } 
     });