setOnPageChangeListener does not call onPageSelected

I’ve setup a simpleonpagechangelistener using the following example:

Android SimpleOnPageChangeListener – Determine swipe direction

  • java.lang.RuntimeException: Method called after release()
  • android listview swipe to delete like in gmail app
  • org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
  • Reposition scroll bar of ListView with padding
  • Android support lib v4 or v13
  • Android - whitelabel app
  • However when I execute the code then swipe the viewpager I would expect the following line to be called:

    public void onPageSelected(int position) {
    

    However it never seems to do so – which is causing a non-functional viewPager.

    Any suggestions?

       public class Home extends YouTubeBaseActivity implements VideoClickListener {
        ...
        private int mCurrentTabPosition = NO_CURRENT_POSITION;
        private static final int NO_CURRENT_POSITION = -1;
        private ViewPager mPager;
        ...
        int imageArray[];
        String[] stringArray;
    
        private OnPageChangeListener mPageChangeListener;
        ImagePagerAdapter adapter = new ImagePagerAdapter();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home);
    
            final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    
            viewPager.setAdapter(adapter);
    ...
    
            setOnPageChangeListener(mPageChangeListener);
    
    
            mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
    
    
                @Override
                public void onPageSelected(int position) {
                    onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
                    position = mCurrentTabPosition;
                    int oldPos = viewPager.getCurrentItem();
                    if (position > oldPos) {
    
                        System.out.print(position);
                        // Moving to the right
                        String playlist = "TheMozARTGROUP‎";
                        new GetYouTubeUserVideosTask(responseHandler, playlist)
                                .execute();
    
                        View vg = findViewById(R.layout.home);
                        vg.invalidate();
    
                    } else if (position < oldPos) {
                        // Moving to the Left
                        System.out.print(position);
                        String playlist = "TheMozARTGROUP‎";
                        new GetYouTubeUserVideosTask(responseHandler, playlist)
                                .execute();
    
                        View vg = findViewById(R.layout.home);
                        vg.invalidate();
    
                    }
                    viewPager.setOnPageChangeListener(mPageChangeListener);
                }
    
                private void onTabChanged(PagerAdapter adapter,
                        int mCurrentTabPosition, int position) {
                    // TODO Auto-generated method stub
    
                }
            };
    
        }
    
        private void setOnPageChangeListener(
                OnPageChangeListener mPageChangeListener2) {
            // TODO Auto-generated method stub
    
        }
    
        ...
    
    
    
        private class ImagePagerAdapter extends PagerAdapter {
            public ImagePagerAdapter(Activity act, int[] mImages,
                    String[] stringArra) {
                imageArray = mImages;
                activity = act;
                stringArray = stringArra;
            }
    
    
            public ImagePagerAdapter() {
                super();
    
            }
    
            private int[] mImages = new int[] { R.drawable.selstation_up_btn,
                    R.drawable.classical_up_btn, R.drawable.country_up_btn,
                    R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
                    R.drawable.island_up_btn, R.drawable.latin_up_btn,
                    R.drawable.pop_up_btn, R.drawable.samba_up_btn };
    
            private String[] stringArray = new String[] { "vevo",
                    "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
                    "EminemVEVO‎" };
    
            @Override
            public int getCount() {
                return mImages.length;
            }
    
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == ((ImageView) object);
            }
    
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                Context context = Home.this;
                ImageView imageView = new ImageView(context);
                imageView.setScaleType(ScaleType.FIT_XY);
                imageView.setImageResource(mImages[position]);
                ((ViewPager) container).addView(imageView, 0);
                return imageView;
            }
    
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ((ViewPager) container).removeView((ImageView) object);
            }
    
            protected void onTabChanged(final PagerAdapter adapter,
                    final int oldPosition, final int newPosition) {
                // Calc if swipe was left to right, or right to left
                if (oldPosition > newPosition) {
                    // left to right
                } else {
                    // right to left
    
    
                }
                final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    
            }
        }
    }
    

    XML:

     <android.support.v4.view.ViewPager
      android:id="@+id/view_pager"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent" />
    

    Related posts:

    How to add a button at the end of RecyclerView?
    How to get Specific Contact Number by using Contact Id
    How can I align an image inside an ImageView to the bottom of the ImageView?
    SAX parser vs XMLPull parser
    Showing gif in android
    Call (and get the response for) a USSD Code, in the background?
  • How can I find the number of years between two dates in android?
  • UnknownError: ApiException:Key hash **** does not match… when I have FB app installed
  • OpenStreetMap in Android
  • Getting Direction Vector in Android
  • Android 4.4 Virtual Device Internal Storage Will Not Resize
  • How to reference an icon from a Library Project in Application's AndroidManifest.xml
  • 5 Solutions collect form web for “setOnPageChangeListener does not call onPageSelected”

    Use

     viewPager.setOnPageChangeListener(mPageChangeListener);
    

    Remove

     private void setOnPageChangeListener(
            OnPageChangeListener mPageChangeListener2) {
        // TODO Auto-generated method stub
    
    }
    

    and

     setOnPageChangeListener(mPageChangeListener);
    

    I was using ViewPagerIndicator and it turns out that when you do that, you should set the OnPageChangeListener on the indicator instead of on the viewpager. (as documented)

    So do:

        mIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                doYourThing();
            }
        });
    

    instead of:

        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                doYourThing();
            }
        });
    

    The same thing applies if you’re using SlidingTabLayout

    Did you tried to use a ViewPager.setOnPageChangeListener instead your SimpleChangeListener? just try it:

    mPager.setOnPageChangeListener(new OnPageChangeListener() {
    
            @Override
            public void onPageSelected(int pos) {
    
            }
    
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onPageScrollStateChanged(int pos) {
    
            }
        });
    

    I had the same problem now – I also set a variable as a equal to:

     mPageChangeListener =  new ViewPager.OnPageChangeListener() {<methods>}
    

    and then assigned it to my viewpager afterwards by:

     mPager.setOnPageChangeListener(mPageChangeListener)
    

    The reason why your code doesn’t work is because you are creating your local variable inside your onCreate method. Two ways you can fix this:

    1. You can assign your code for the onPageChangeListener inside your setter so:

    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {<methods>})

    1. you can create a member variable for your ‘mPageChangeListener’ so that it will be initialised before onCreate is called. So copy your mPageChangeListener code above your onCreate method and initialise there.
            viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    
                public void onPageSelected(int currentPage) {
                    // work here after changing page
                }
    
            });
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.