How to test if a fragment view is visible to the user?

前端 未结 10 562
闹比i
闹比i 2020-11-28 04:42

I have a ViewPager, each page is a Fragment view. I want to test if a fragment is in a visible region. the Fragment.isVisible only test

  • the fragment is attache
10条回答
  •  心在旅途
    2020-11-28 05:15

    This is what I use to determine the visibility of a fragment.

    private static boolean m_iAmVisible;
    
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) { 
        super.setUserVisibleHint(isVisibleToUser);
        m_iAmVisible = isVisibleToUser;
    
        if (m_iAmVisible) { 
            Log.d(localTAG, "this fragment is now visible");
        } else {  
            Log.d(localTAG, "this fragment is now invisible");
        }
    }
    

提交回复
热议问题