ViewPager call setUserVisibleHint after first Fragment is loaded

后端 未结 6 1409
梦谈多话
梦谈多话 2020-12-13 23:40

In my application, I have a ViewPager which holds many swipeable Tabs with Fragments inside. I use the setUserVisibleHint method to detect when a Fragment

6条回答
  •  自闭症患者
    2020-12-14 00:32

    Well, its workaround more, and maybe its better to setup OnPageChangeListener, but it works for me. The task is to determine whether the current fragment is visible at creation. You can use the menu to do this. In your fragment:

     @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true); // set current fragment has its own options menu
        } 
    

    and this

     @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         ...
    
                if(isMenuVisible()) { // menu should be visible if current fragment is visible right now
                    setUserVisibleHint(true); // manually set value to true
                }
         ...
        return view;
    

    Maybe someone will find it usefull. This works for me.

提交回复
热议问题