Android: How to Check If a View Inside of Scrollview Is Visible

Android: how to check if a View inside of ScrollView is visible?

Use View#getHitRect instead of View#getDrawingRect on the view you're testing. You can use View#getDrawingRect on the ScrollView instead of calculating explicitly.

Code from View#getDrawingRect:

 public void getDrawingRect(Rect outRect) {
outRect.left = mScrollX;
outRect.top = mScrollY;
outRect.right = mScrollX + (mRight - mLeft);
outRect.bottom = mScrollY + (mBottom - mTop);
}

Code from View#getHitRect:

public void getHitRect(Rect outRect) {
outRect.set(mLeft, mTop, mRight, mBottom);
}

Check a child view visible on screen or not in NestedScrollview

You can use next code:

Rect mReact = new Rect();
scrollView.getHitRect(mReact);
if (mView.getLocalVisibleRect(mReact)) {
// visible
} else {
// invisible
}

Determine which view items are visible and most centered in a horizontal scrollview in android?

to determine if a View is visible you can do this like Bill Mote

 Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
// Any portion of the imageView, even a single pixel, is within the visible window
} else {
// NONE of the imageView is within the visible window
}

is stated as answer to this question Android: how to check if a View inside of ScrollView is visible?

there you can play to see which one is more centered with a little math and the order in which you added your views



Related Topics



Leave a reply



Submit