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

后端 未结 14 1891
自闭症患者
自闭症患者 2020-11-22 16:42

I have a ScrollView which holds a series of Views. I would like to be able to determine if a view is currently visible (if any part of it is curre

14条回答
  •  粉色の甜心
    2020-11-22 17:22

    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);
    }
    

提交回复
热议问题