2012-01-03 4 views
0
/** 
* Defines an interface for a callback that will handle 
* responses from the thread loader when an image is done 
* being loaded. 
*/ 
public interface ImageLoadedListener { 
    public void imageLoaded(Bitmap imageBitmap); 
} 

なので、その後どこかにコード空のリスナーの機能と何用法、それは空のスタブ

// If in the cache, return that copy and be done 
       if(Cache.containsKey(item.url.toString()) && Cache.get(item.url.toString()) != null) { 
        // Use a handler to get back onto the UI thread for the update 
        handler.post(new Runnable() { 
         public void run() { 
          if(item.listener != null) { 
           // NB: There's a potential race condition here where the cache item could get 
           //  garbage collected between when we post the runnable and it's executed. 
           //  Ideally we would re-run the network load or something. 
           SoftReference<Bitmap> ref = Cache.get(item.url.toString()); 
           if(ref != null) { 
            item.listener.imageLoaded(ref.get()); 
           } 
          } 
         } 
        }); 
       } else { 
        final Bitmap bmp = readBitmapFromNetwork(item.url); 
        if(bmp != null) { 
         Cache.put(item.url.toString(), new SoftReference<Bitmap>(bmp)); 

         // Use a handler to get back onto the UI thread for the update 
         handler.post(new Runnable() { 
          public void run() { 
           if(item.listener != null) { 
            item.listener.imageLoaded(bmp); 
           } 
          } 
         }); 
        } 

       } 

での私の質問はimageLoaded(ビットマップimageBitmap)は、それが提供して除いて何もしない空の関数であります折り返し電話。だから、item.listener.imageLoaded(ref.get());それの意義は何ですか?どこにつながるのか? imageLoadedは空のスタブ関数であるためです。 item.listener.imageLoaded(bmp)を使用したサミングそれはどこにもつながっていないようです。

答えて

2

ImageLoadedListenerはインターフェイスです。このインターフェイスの実装では、画像がロードされたときに必要な処理を行うために、imageLoaded()という独自の実装を提供できます。

関連する問題