Ho un HorizontalScrollView che contiene un LinearLayout per contenere tutta la mia vista. Aggiungo circa 20 RelativeLayout che contiene un ImageView e TextView al LinearLayout. Vorrei caricare le immagini solo se ImageView è sullo schermo (mentre scorro verso ImageView).Android scaricano solo le immagini che sono visibili in orizzontale. CrollView
Ho provato a seguire this post per utilizzare getHitRect()
, sulla miniatura, tuttavia, il Rect (limiti) per la miniatura è sempre 0, 0-0, 0, con conseguente metodo My per restituire false. Che cosa sto facendo di sbagliato?
thumbnailLayout
è il mio LinearLayout all'interno del HorizontalScrollView
thumbnailScroll
è il mio 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);
}
Edit sto usando TRED TreeObserver e ha scoperto che il mio metodo per controllare se le thumbails sono sullo schermo è sbagliata. Si afferra ancora tutte le immagini, e costantemente loop (perché sto utilizzando 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;
}
});
Dove stai chiamando da cui Runnable? È nell'ascoltatore di scroll? – dmon
Im eseguirlo dopo aggiungo ImageViews al LinearLayout. Stavo pensando dopo che le immagini sono state aggiunte al LinearLayout, alcune immagini dovrebbero essere caricate. Quindi userò l'ascoltatore scroll – heero
Potresti usare una Gallery con un adattatore? – Ethan