ViewPager call setUserVisibleHint after loading the first fragment

In my application, I have a ViewPager that contains many scrollable tabs with fragments inside. I use the setUserVisibleHint method to detect when a Fragment appears on the screen. This works great when the user clicks between the tabs, but he does not work on the first boot. To run the code in the method, I have to scroll left and then return to the first Fragment , because the setUserVisibleHint method setUserVisibleHint called before the onCreateView method.

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

+30
android android-fragments android-viewpager
Sep 04 '14 at 2:09
source share
6 answers

You cannot (and should not) rely on setUserVisibleHint for this. Instead, you should use ViewPager.OnPageChangeListener to receive callbacks when the page becomes visible. For example.

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

Note. You can use ViewPager.SimpleOnPageChangeListener if you do not need to listen to all callbacks.

Update

setOnPageChangeListener now deprecated, use addOnPageChangeListener instead

 viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { // do your work } }); 
+46
Sep 13 '14 at 11:37
source share

WORK BELOW FOR ME

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) { // do your stuff here. } } 
+3
Mar 13 '17 at 17:42
source share

You must manually call mViewPagerListener.onPageSelected(0); in onCreateView in root activity / fragment.

+2
Sep 12 '14 at 11:13
source share

I don’t understand what you need exactly, but you can detect the page of the current fragment by calling ViewPager.getCurrentItem , which return an integer.

Hope this helps.

Good luck.

0
Sep 09 '14 at 5:41
source share

Well, his workaround is bigger, and it might be better to configure OnPageChangeListener , but it works for me. The challenge is to determine if the current fragment is visible at creation. You can use this menu for this. In your snippet:

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

and this one

  @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 this helpful. This works for me.

0
Jun 12 '17 at 14:30
source share

I check your problem, but I find out that the onCreateView method onCreateView called before the setUserVisibleHint method

 enter code here 

because the setUserVisibleHint method is called before the onCreateView method

-3
Jan 23 '15 at 5:11
source share



All Articles