-1

I have a ViewPager of 3 fragments. When every I switch to my third fragment, touching it in places where the previous fragment had touch events is activating those events.

Example: When I set the current item to 2 (Third fragment, Menu) and I click in the center, the button that I have in the center of item 1 (Second fragment, Intro) gets activated.

Edit: I just noticed that this happens not only to the previous fragment but the next one as well. When I was in item 1 I triggered an event in item 2.

This is my Adapter:

/**
 * A simple pager adapter that represents 3 ScreenSlidePageFragment objects,
 * in sequence.
 */
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            Fragment frag = new SetupFragment(SplashActivity.this, step);
            return frag;
        case 1:
            Fragment frag2 =new IntroFragment(SplashActivity.this);
            return frag2;
        case 2:
            Fragment frag3 = new MenuFragment(SplashActivity.this);
            return frag3;
        default:
            throw new RuntimeException("No item at position " + position);
        }
    }

    @Override
    public int getCount() {
        return 3;
    }
}

Does anyone know a reason for this happening?

Display name
  • 1,761
  • 2
  • 14
  • 16

1 Answers1

0

This was due to me overriding certain methods in the ViewPager

This is what I did:

public class NonSwipeableViewPager extends ViewPager {

    private boolean isPagingEnabled = true;

    public NonSwipeableViewPager(Context context) {
        super(context);
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }

    public boolean isPagingEnabled() {
        return isPagingEnabled;
    }
}

This is wrong, and is not the way to block paging. (I found that in an answer somewhere on stack overflow)

Instead I returned false in both methods that I overrided and it works flawlessly.

Display name
  • 1,761
  • 2
  • 14
  • 16