How to determine when Fragment becomes visible in ViewPager

后端 未结 26 1322
心在旅途
心在旅途 2020-11-22 00:24

Problem: Fragment onResume() in ViewPager is fired before the fragment becomes actually visible.

For example, I have 2 fragments with

26条回答
  •  醉话见心
    2020-11-22 00:59

    Here is another way using onPageChangeListener:

      ViewPager pager = (ViewPager) findByViewId(R.id.viewpager);
      FragmentPagerAdapter adapter = new FragmentPageAdapter(getFragmentManager);
      pager.setAdapter(adapter);
      pager.setOnPageChangeListener(new OnPageChangeListener() {
    
      public void onPageSelected(int pageNumber) {
        // Just define a callback method in your fragment and call it like this! 
        adapter.getItem(pageNumber).imVisible();
    
      }
    
      public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
    
      }
    
      public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
    
      }
    });
    

提交回复
热议问题