Android only loads images that are visible in horizontalScrollView

I have a HorizontalScrollView that contains a LinearLayout to store my entire view. I am adding about 20 RelativeLayout which contains ImageView and TextView for LinearLayout. I would like to download only images if the ImageView is on the screen (when scrolling to ImageView).

I tried to use this post to use getHitRect() in thumbnail, however, Rect (bounds) for thumbnail is always 0, 0-0, 0, as a result my method returns false. What am I doing wrong?

thumbnailLayout is my LinearLayout inside a HorizontalScrollView
thumbnailScroll - my 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 I use TreeObserver and found that my method of checking the correct display of files on the screen is incorrect. It still captures all images and constantly loops (because I'm using 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; } }); 
+6
source share
2 answers

You need a ListView, but android does not have its own HorizontallListView. You can try the HorizontallListView from DevSmart . It works as its own list, with adapters and custom views, if you want.

Yo lo estoy utilizando en mi proyecto por la misma razΓ³n que tu, para cargar solo las imagenes visibles, so I hope this can help you.

0
source

Have you thought about using ViewPager? http://developer.android.com/reference/android/support/v4/view/ViewPager.html

If this doesn't fit your design, you need to redefine the ScrollView β†’ onScrollChanged function and check if your image is displayed on the screen at the image position (left?) Compared to the scroll position.

0
source

All Articles