Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager call setUserVisibleHint after first Fragment is loaded

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 comes to the screen. This works great when the user swipes between tabs but it does not work on the first load. To run the code in the method I have to swipe to left and then back to the first Fragment because the setUserVisibleHint method is called before the onCreateView method.

Do you have any ideas how I can run this code after the first Fragment is visible? Is there a method in the ViewPager or something else?

like image 218
Cilenco Avatar asked Sep 04 '14 14:09

Cilenco


People also ask

How do you know when a fragment becomes visible?

fragment:fragment:1.1. 0 you can just use onPause() and onResume() to determine which fragment is currently visible for the user. onResume() is called when the fragment became visible and onPause when it stops to be visible. To enable this behavior in the first ViewPager you have to pass FragmentPagerAdapter.

When should I use ViewPager?

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .


2 Answers

You can't (and shouldn't) rely on setUserVisibleHint for this. Instead, you should be using a ViewPager.OnPageChangeListener to get callbacks for when a page becomes visible. E.g.

viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {     @Override     public void onPageSelected(int position) {         // do your work     }  }); 

Note: You can use ViewPager.SimpleOnPageChangeListener if you don't need to listen for all callbacks.

Update

setOnPageChangeListener method is now deprecated, use addOnPageChangeListener instead

viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {     @Override     public void onPageSelected(int position) {         // do your work     }  }); 
like image 185
Paul Burke Avatar answered Oct 20 '22 17:10

Paul Burke


BELOW WORKED FOR ME

Please create a global view like this

View view;      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,                              Bundle savedInstanceState)     {         //inflate view layout         view =inflater.inflate(R.layout.your_fragment, container, false);          // return view         return view;     } 

and use this

 @Override         public void setUserVisibleHint(boolean isUserVisible)         {             super.setUserVisibleHint(isUserVisible);            // when fragment visible to user and view is not null then enter here.                 if (isUserVisible && view != null)                 {                    onResume();                 }          } 

and In OnResume method

@Override          public void onResume() {           super.onResume();                if (!getUserVisibleHint()) {             return;           }       //do your stuff here    } 
like image 31
Rahul Avatar answered Oct 20 '22 15:10

Rahul