Fragment:setUserVisibleHint方法和onHiddenChanged方法对比

setUserVisibleHint()

适用于和ViewPager+Fragment组合使用,此方法与FragmentPagerAdapter类相关,可看如下代码:

 public Object instantiateItem(ViewGroup container, int position) {
 
        ...
        
        if (fragment != mCurrentPrimaryItem) {
            fragment.setMenuVisibility(false);
            fragment.setUserVisibleHint(false);
        }

        return fragment;
    }

public void setPrimaryItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment)object;
        if (fragment != mCurrentPrimaryItem) {
            if (mCurrentPrimaryItem != null) {
                mCurrentPrimaryItem.setMenuVisibility(false);
                mCurrentPrimaryItem.setUserVisibleHint(false);
            }
            if (fragment != null) {
                fragment.setMenuVisibility(true);
                fragment.setUserVisibleHint(true);
            }
            mCurrentPrimaryItem = fragment;
        }
    }

onHiddenChanged(boolean hidden)

仅当调用了hide()或者show()才回调此方法。

你可能感兴趣的:(Andoird)