How to know when fragment actually visible in viewpager

后端 未结 4 1567
走了就别回头了
走了就别回头了 2021-01-12 16:41

I am using 4 fragments inside a ViewPager ,as ViewPager load the previous and next fragment in advance ,and no lifecycle method is called when navigating between fragment

4条回答
  •  一向
    一向 (楼主)
    2021-01-12 16:54

    You can use viewPager.getCurrrentItem() to get the currently selected index, and from that you should be able to extrapolate which fragment is shown. However what you probably want is to use addOnPageChangeListener() to add an OnPageChangeListener. This will let you keep track of what page is selected, as it's selected by implementing the onPageSelected(int selected) method.

提交回复
热议问题