Mert Serimer
Mert Serimer

Reputation: 1245

How to disable Specific Page Swipe in ViewPager

I have a ViewPager widget in my app with 5 fragments. I have 3 buttons for going to pages manually. For the 4 and 5 pages, i want user to be able to go only by pressing a button not by swipe. So i need to disable swipe events THROUGH them but not for others. Also when in page 4 or 5, swiping will be disabled completely and back button will appear on actionbar. How to achieve it?

List<Fragment> l = new ArrayList<>();
    l.add(Fragment.instantiate(this, Osman.class.getName()));
    l.add(Fragment.instantiate(this,Ahmet.class.getName()));
    l.add(Fragment.instantiate(this,Kurik.class.getName()));
    l.add(Fragment.instantiate(this,Sorok.class.getName()));
    l.add(Fragment.instantiate(this,Yah.class.getName()));
    SliderAdapter sa = new SliderAdapter(getSupportFragmentManager(),l);
    vp = (ViewPager) findViewById(R.id.viewPager);
    vp.setAdapter(sa);

Upvotes: 2

Views: 3334

Answers (1)

Logain
Logain

Reputation: 4364

Follow this question in order to be able to disable swiping at will How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

And combine that with your SliderAdapter in order to control which pages will be swipeable or not.

If you followed the link above, on your subclass of ViewPager, you can add some GestureDetector on onTouchEvent(MotionEvent event) and feed it with your event, like Android: How to handle right to left swipe gestures

By doing this, you will now have some onSwipeRight and manage there your logic, delegating to the adapter the question if the page should be swiped or not, or where to go next.

Upvotes: 2

Related Questions