0

I have an activity with a ViewPager that contains three fragments

    public class Hello extends extends FragmentActivity
...
adapterViewPager.addFragment(new FRG_one());
adapterViewPager.addFragment(new FRG_two());
adapterViewPager.addFragment(new FRG_three());
adapterViewPager.addFragment(new FRG_four());
...

Within the activity, I have a button that when pressed I want to do one action or another, depending on the fragment that is being displayed on the screen

I found ways to detect if a fragment is visible, but do not serve me.

What I need is that the activity that contains the viewpager (extends FragmentActivity) detects fragment has focus

I appreciate the help

regards :)

Sergio76
  • 3,835
  • 16
  • 61
  • 88
  • You mean something like this: http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-viewpager ? – DuKes0mE Apr 26 '13 at 14:56

3 Answers3

4

You can call ViewPager.setOnPageChangeListener(...) to listen for page changes. Your listener can store the current page position in its onPageSelected(int position) callback, then just vary your button action based on that.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
2

Why not use an onPageChangeListener? This will fire a callback when the view pager displays a new page or fragment. You can use the supplied index to determine which fragment is currently in focus.

Bobbake4
  • 24,509
  • 9
  • 59
  • 94
0

Create a loop that goes through all your fragments and check the .isVisible() method if it returns true, if it returns true then break;, else keep going.

TronicZomB
  • 8,667
  • 7
  • 35
  • 50