すべてのビューを保持する LinearLayout を含む HorizontalScrollView があります。ImageView と TextView を含む約 20 の RelativeLayout を LinearLayout に追加します。ImageView が画面上にある場合にのみ画像をロードしたいと思います (ImageView までスクロールすると)。
この投稿に従って、サムネイルでを使用しようとgetHitRect()
しましたが、サムネイルの Rect (境界) は常に 0、0-0、0 であるため、メソッドが false を返します。私は何を間違っていますか?
thumbnailLayout
HorizontalScrollView 内の私の LinearLayout は私の HorizontalScrollView
thumbnailScroll
です
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 を使用していて、サムネイルが画面に表示されているかどうかを確認する方法が間違っていることがわかりました。それでもすべての画像を取得し、常にループします (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;
}
});