Search code examples
androidandroid-viewpager

Disable ViewPager Swipe


I'm in a class extending a FragmentActivity. To disable the viewpager swipe i wan't to override the onInterceptTouchEvent method which is not possible because i can't extend the viewpager class because i'm already extending the FragmentActivity class.

A solution for this problem would be to create a custom viewpager:

public class CustomViewPager extends ViewPager
{
    private boolean enabled;
    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
            return this.enabled && super.onInterceptTouchEvent(event);
    }
}

and

 CustomViewPager viewPager;
 viewPager = findViewById(R.id.viewpager);

but then i get the following casting error:

android.support.v4.view.ViewPager cannot be cast to MainPage$CustomViewPager

are there some other ways to override the onInterceptTouchEvent method or to disable the view pager swipe?


Solution

  • Just write;

    viewPager = (CustomViewPager) findViewById(R.id.viewpager);
    

    The findViewById method returns view, you have to say which class will be initialized.