0

I have a view pager with 2 fragments. One of them has a blue screen that should be hidden once the fragment is selected. So, I want to grasp a pointer to this fragment that I can call a certain method on once the user selects this fragment. I have implemented the addOnPageChangeListener interface. I want to call that method, to hide the blue screen, in the onPageSelected call.

My question is how can I grasp a pointer to the current selected fragment?

I have tried the following:

AnswersFragment fragment = (AnswersFragment)   getSupportFragmentManager().findFragmentByTag("android:switcher:" +     this.pager + ":" + position);
    if (fragment == null)
        fragment = new AnswersFragment();

But it always returns null.

I have also tried to keep an instance variable called currentAnswerFragment, but it doesn't necessarily point to the current fragment as the view pager keeps track of 3 fragments at the same time.

Also, my model is not an array (or 2) of fragments. I call a function that initializes a fragment and return it. With that said, I can't use the index of the current item to get the fragment being displayed.

Essam Ewaisha
  • 439
  • 5
  • 15

2 Answers2

0

You need to override onPageSelected method and fire respected method on desired screen, for example:

@Override
public void onPageSelected(int position) {
    if (position == 0) {
       callForPage();
    } else if (position == 1) {
        callForPage1();
    }
}

and you can use following code to go to selected screen.

viewPager.setCurrentItem(pageposition,false);

setCurrentItem takes two parameters: item number and boolean for smooth scrolling.

UPDATE

make a PageAdapter class for example

  public class PagerAdapter extends FragmentPagerAdapter {

  Context mcontext;

  public PagerAdapter(FragmentManager fm, Context context) {
super(fm);
mcontext = context;
}

 @Override
 public Fragment getItem(int position) {
if (position == 0) {
    return new FragmentA();
}
else if(position==1){
    return new FragmentB();
}
else
    return new FragmentC();
}

 @Override
 public int getCount() {
  return 3;
}
}

Then in your Activity class

mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), this));
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        ///Here you handle the pointer thing 
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
  • Yeah that's what I am trying to do. But, the problem is I don't have a pointer to the currently displayed fragment. So, how can I get the current fragment? – Essam Ewaisha Dec 30 '15 at 14:31
  • Make a custom page adapter class. then set adapter to viewpager. if i got it right. Let me edit the answer. – Zeeshan Shabbir Dec 30 '15 at 14:40
  • Yeah that's what I have right now. But the problem is: Let's say fragment at position 1 is on the screen. In the onPageSelected, I wanna call a method on fragmentB (the instance visible on the screen). How can I get the fragment to call the method on? If I called the adapter.getItem(1), it will return a new instance of fragmentB. Is there something I am missing? – Essam Ewaisha Dec 30 '15 at 15:48
  • why would you do that? – Zeeshan Shabbir Dec 31 '15 at 05:07
  • I guess you are trying to get some values from fragment B while you are on Fragment A? right? – Zeeshan Shabbir Dec 31 '15 at 05:07
  • In some cases yes. But, in this case, all what I am only trying to do is to notify the fragment being displayed that it is actually being displayed now (It has been selected). I figured out a solution. Thanks a ton for the help! – Essam Ewaisha Jan 01 '16 at 11:04
  • post your solution please! – Zeeshan Shabbir Jan 01 '16 at 11:33
  • I have posted it. Check it out and please tell me if it's not clear, to explain more. – Essam Ewaisha Jan 01 '16 at 16:09
0

Edit: A better implementation of the solution I came up with is found here

After a while of debugging, I figured out a solution. By keeping an external data structure of the previous fragments visited. In my case I kept a HashMap as a normal array won't work. In some cases I call the pager.setCurrentItem(position). Meaning that I jump fragments. So a HashMap is more appropriate.

Community
  • 1
  • 1
Essam Ewaisha
  • 439
  • 5
  • 15