Phantom Phantom - 6 months ago 34
Android Question

Get the position of the ViewPager on first and last visible fragment

I implemented in my app a ViewPager to slide between fragments. After a long research I discovered that if I want to get the position of the visible fragment I should use:


But for some reason, when I'm on the first or the last fragment, the position doesn't change. It remains 1 on the first fragment, instead of 0, respectively size - 2 for the last, instead of size - 1. (In case of the first fragment, the theory is applicable only when I start the ViewPager from a different position from the first)

How can I get the first and the last position of the ViewPager. I even tried to send the position from the ViewPagerAdapter, but here I am dealing with the same thing, as the last changing of the position is happening before the first or the last fragment.

Any suggestions ?


In YourPagerActivity.class add listener in create method

    viewPager = (ViewPager) findViewById(;
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        public void onPageSelected(int position) {
            String positions=position + "";

            Toast.makeText(YourPagerActivity.this, positions, Toast.LENGTH_SHORT).show();

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        public void onPageScrollStateChanged(int state) {