android – ViewPager set current page programmatically-ThrowExceptions

Exception or error:

I wrote a custom ViewPager to disable Swipe Scroll, but I want to swipe programmatically. I have three Tab in my view pager, but when I call viewPager.setCurrentItem(viewPager.getCurrentItem()+1) on the first Fragment, it moves to the third Fragment instead of the second Fragment. And if I call same function in the second Fragment, it goes to the third. If I call (viewPager.getCurrentItem()-1)` in the third fragment, it works fine by moving back. Any help would be appreciated. My code is below:

NonSwipeAbleViewPager

public class NonSwipeableViewPager extends ViewPager {

private boolean swipeable;

public NonSwipeableViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPager);
    try {
        swipeable = a.getBoolean(R.styleable.MyViewPager_swipeable, true);
    } finally {
        a.recycle();
    }
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    return swipeable ? super.onInterceptTouchEvent(event) : false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return swipeable ? super.onTouchEvent(event) : false;
}
}

Declaration in XML

<co.example.customview.NonSwipeableViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:swipeable="false" />

Calling it

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menuNext:
            NonSwipeableViewPager pages = (NonSwipeableViewPager) getActivity().findViewById(R.id.pager);
            pages.setCurrentItem(pages.getCurrentItem()+1, true);
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
}
How to solve:

In your PagerActivity use

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            int pagei = position + 1;
            pages=pagei + "";

            Toast.makeText(PagerActivity.this, getString(R.string.changeinfopage) + " " + pages, Toast.LENGTH_SHORT).show();
        }

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

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

and

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        switch (item.getItemId()) {

            case R.id.action_previous:

                viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
                return true;

            case R.id.action_next:

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                return true;
        }

        return super.onOptionsItemSelected(item);

   }

###

viewPager.setCurrentItem(idx);

where idx is 0 based integer.

###

try this :

  viewPager.postDelayed(new Runnable() {

    @Override
    public void run() {
        viewPager.setCurrentItem(position);
    }
}, 10);

sometimes, setCurrentItem on viewpager doesn’t work. As pager’s content was controlled by a spinner. both the spinners and the pagers state were restored onResume, and because of this the spinners onItemSelected listener was called during the next event propagation cycle.

By using handler we can make this work because it set the pagers current position after the onItemSelected event fired.

Leave a Reply

Your email address will not be published. Required fields are marked *