我有一個HorizontalScrollView,其中包含一個LinearLayout來容納我的所有視圖。我向LinearLayout添加了大約20個包含ImageView和TextView的RelativeLayout。我想僅在ImageView在屏幕上時加載圖像(當我滾動到ImageView時)。Android只下載horizontalScrollView中可見的圖像
我試圖在this post之後在縮略圖上使用getHitRect()
,但是縮略圖的Rect(bounds)始終爲0,0-0,0,導致我的方法返回false。我究竟做錯了什麼?
thumbnailLayout
是我HorizontalScrollView
thumbnailScroll
內的LinearLayout是我HorizontalScrollView
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;
}
});
你從哪裏調用Runnable?它在滾動監聽器中嗎? – dmon
在將ImageView添加到LinearLayout後,我正在運行它。我在imageViews被添加到LinearLayout後想,一些圖像將不得不加載。然後我會使用滾動監聽器 – heero
您可以使用適配器的圖庫嗎? – Ethan