2

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);
Mert Serimer
  • 1,217
  • 2
  • 16
  • 38

1 Answers1

2

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.

Community
  • 1
  • 1
Logain
  • 4,259
  • 1
  • 23
  • 32
  • I was able to achieve the desired thing in that link in my program. But i couldn't achieve if "specific page && "swipe to right" event check. Because if it is last swipe possible change i need to disable swipe to right but not left! – Mert Serimer Feb 06 '15 at 07:47
  • What did you try? In your adapter you have full control over which page you return. You can add it all the behavior you want. – Logain Feb 06 '15 at 07:50
  • how(or where) to check swipe to right event? In my custom class or in activity where viewpager exists? Also how to check swipe right event? – Mert Serimer Feb 06 '15 at 07:52
  • You can use a gesture detector on your onTouchEvent, and feed it with MotionEvent – Logain Feb 06 '15 at 07:55
  • That gesture detector will be in Activity or customPager class? Also if in activity, will it be for activity override or CustomViewPager variable.gesture bla bla? – Mert Serimer Feb 06 '15 at 08:02
  • Just edited my answer linking to a gesture detector response, I suppose it will answer those questions. – Logain Feb 06 '15 at 08:03