Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager onPageScrolled event not called

The following code scrolls the 4 pages just fine. My problem is that the onPageScrolled event does not get triggered when the pages are scrolled. This occurs whether or not I use the myPager.SetOffsccreenPageLimit(4) command. After reading the docs, it is my understanding that when I have implements ViewPager.OnPageChangeListener included in the MyPagerAdapter class that the event should be triggered. What am I doing wrong?

Thanks, Gary Blakely

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.mysevenpanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
    myPager.setOffscreenPageLimit(4);
}

private class MyPagerAdapter extends PagerAdapter 
    implements ViewPager.OnPageChangeListener {

    public int getCount() {
        return 4;
    }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.left;
            break;
        case 1:
            resId = R.layout.gps;
            break;
        case 2:
            resId = R.layout.map;
            break;
        case 3:
            resId = R.layout.right;
            break;
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }
    @Override
    public void onPageScrollStateChanged(int arg0) {
        int myint = 9;
        myint = myint * 2;
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        int myint = 9;
        myint = myint * 2;
    }

    @Override
    public void onPageSelected(int arg0) {
        int myint = 9;
        myint = myint * 2;
    }
}
like image 844
Dean Blakely Avatar asked Aug 17 '12 19:08

Dean Blakely


2 Answers

You also need to set the listener using setOnPageChangeListener().

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.mysevenpanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
    myPager.setOffscreenPageLimit(4);
    myPager.setOnPageChangeListener(adapter); // This line here
}
like image 113
Cat Avatar answered Nov 03 '22 06:11

Cat


I believe that you also need to register your Adapter instance as a listener for those types of events too using .setOnPageChangeListener() on the ViewPager.

like image 1
Zsombor Erdődy-Nagy Avatar answered Nov 03 '22 07:11

Zsombor Erdődy-Nagy