2013-04-18 76 views
6

我有一個Horizo​​ntalScrollView,其中包含一個LinearLayout來容納我的所有視圖。我向LinearLayout添加了大約20個包含ImageView和TextView的RelativeLayout。我想僅在ImageView在屏幕上時加載圖像(當我滾動到ImageView時)。Android只下載horizo​​ntalScrollView中可見的圖像

我試圖在this post之後在縮略圖上使用getHitRect(),但是縮略圖的Rect(bounds)始終爲0,0-0,0,導致我的方法返回false。我究竟做錯了什麼?

thumbnailLayout是我Horizo​​ntalScrollView
thumbnailScroll內的LinearLayout是我Horizo​​ntalScrollView

runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
      Log.e(TAG, "running"); 
      for (int i = 0; i < thumbnailLayout.getChildCount(); i++) { 
       RelativeLayout view = (RelativeLayout) thumbnailLayout.getChildAt(i); 
       ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail); 
       if (thumbnailIsOnScreen(thumbnail)) { 
        Log.e(TAG, items.get(i).getTitle() + " has downloaded"); 
        app.setImage(items.get(i).getThumbnailSmall(), thumbnail); 
       } 
      } 


     } 
    }); 

private boolean thumbnailIsOnScreen(ImageView thumbnail) { 
     Rect bounds = new Rect(); 
     thumbnail.getHitRect(bounds); 

     Rect scrollBounds = new Rect(thumbnailScroll.getScrollX(), thumbnailScroll.getScrollY(), 
       thumbnailScroll.getScrollX() + thumbnailScroll.getWidth(), thumbnailScroll.getScrollY() 
         + thumbnailScroll.getHeight()); 
     return Rect.intersects(scrollBounds, bounds); 
    } 

編輯 我使用TreeObserver TRED,發現我的方法來檢查thumbails是在屏幕上是錯誤的。它仍然抓住所有的圖像,並不斷循環,(因爲我使用onPreDrawListener?)

thumbnailLayout.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() { 

      @Override 
      public boolean onPreDraw() { 
       Log.e(TAG, "running"); 

       for (int i = 0; i < thumbnailLayout.getChildCount(); i++) { 
        RelativeLayout view = (RelativeLayout) thumbnailLayout.getChildAt(i); 
        ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail); 
        if (thumbnailIsOnScreen(thumbnail)) { 
         Log.e(TAG, items.get(i).getTitle() + " has downloaded"); 
         app.setImage(items.get(i).getThumbnailSmall(), thumbnail); 
        } 
       } 
       return true; 
      } 
     }); 
+0

你從哪裏調用Runnable?它在滾動監聽器中嗎? – dmon

+0

在將ImageView添加到LinearLayout後,我正在運行它。我在imageViews被添加到LinearLayout後想,一些圖像將不得不加載。然後我會使用滾動監聽器 – heero

+0

您可以使用適配器的圖庫嗎? – Ethan

回答

0

你需要一個ListView,但沒有原生Horizo​​ntallListView在android系統。你可以試試HorizontallListView by DevSmart。它像本機列表視圖一樣工作,如果需要,可以使用適配器和自定義視圖。

你好,我希望這可以幫助你。

相關問題