0

I tried placing the interface in my Activity, my fragment and my FragmentStatePagerAdapter. But in all of them the code never got there. this is what my FragmentStatePagerAdapter looks like since this is my latest try.

FragmentStatePagerAdapter :

public static class AppSectionsPagerAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener
    {

     ......
     ......
    @Override
    public void onPageScrollStateChanged(int arg0)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0)
    {
        // TODO Auto-generated method stub
        Log.w("onpage", "onpage executed");
        currentSelectedFragmentPosition = arg0;
    }

    public int getCurrentSelectedFragmentPosition()
    {
        return currentSelectedFragmentPosition;
    }
}

Fragment would look like this :

  public class SectionFragment extends ListFragment implements OnPageChangeListener
        {

     ......
     ......
    @Override
    public void onPageScrollStateChanged(int arg0)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0)
    {
        // TODO Auto-generated method stub
        Log.w("onpage", "onpage executed");
        currentSelectedFragmentPosition = arg0;
    }

    public int getCurrentSelectedFragmentPosition()
    {
        return currentSelectedFragmentPosition;
    }
}

But both didnt work, it would be most usefull to me to use it in a fragment but if that is not possible then the activity or statepager will work too

Shishi
  • 601
  • 2
  • 8
  • 27
  • you should add OnPageChangeListener to viewPager not AppSectionsPagerAdapter because callback come from the viewPager – Santhosh Feb 21 '14 at 13:45
  • I tried that aswell in my fragment like this ((MainActivity).getActivity().setOnPageChangeListener but that didnt work either I tried that in onStart and onCreateView the problem is those don't loop as far as I know so where should I place it? – Shishi Feb 21 '14 at 13:50
  • where's the viewPager created there make write in next line mViewPager.setOnPageChangeListener(this); – Santhosh Feb 21 '14 at 13:55

1 Answers1

0

In AppSectionsPagerAdapter constructor: add this

mViewPager.setOnPageChangeListener(this);

(this assume the ViewPager is an instance variable named mViewPager)

Or, as an alternative in the MainActivity.onCreate() :

mViewPager.setOnPageChangeListener(mAppSectionPageAdapter);

(this assume the AppSectionPageAdapter is in a variable named mAppSectionPageAdapter)

ben75
  • 29,217
  • 10
  • 88
  • 134
  • mViewPager is declared inside the MainActivity but when I try to use it in the `AppSectionsPagerAdapter` it says it is not static etc Im not sure if I should make it static? – Shishi Feb 21 '14 at 13:56
  • In general try to avoid static objects. Why did you make `AppSectionsPagerAdapter ` static ? Any good reason ? – ben75 Feb 21 '14 at 13:58