What i am trying to do: I am trying to Enable/disable swiping in pager programatically when the program is running
Ex: When on the flow if i check for a condition and if it returns true
enable swiping, and if condition returns false
disable swiping.
public class CustomViewPager extends ViewPager { private boolean enabled; public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); this.enabled = true; } @Override public boolean onTouchEvent(MotionEvent event) { if (this.enabled) { return super.onTouchEvent(event); } return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (this.enabled) { return super.onInterceptTouchEvent(event); } return false; } public void setPagingEnabled(boolean enabled) { this.enabled = enabled; } }
Then select this instead of the builtin viewpager in XML
<mypackage.CustomViewPager android:id="@+id/myViewPager" android:layout_height="match_parent" android:layout_width="match_parent" />
You just need to call the "setPagingEnabled" method with "false" and users won't be able to swipe to paginate.
Problem with above methodology: i cannot set the property on the flow, I:e .... I either can enable swiping or disable swiping. But i cannot do this based on condition
A simple solution is to create your own subclass of ViewPager that has a private boolean flag, isPagingEnabled . Then override the onTouchEvent and onInterceptTouchEvent methods. If isPagingEnabled equals true invoke the super method, otherwise return .
There is no built in way to disable swiping between pages of a ViewPager, what's required is an extension of ViewPager that overrides onTouchEvent and onInterceptTouchEvent to prevent the swiping action. To make it more generalised we can add a method setSwipePagingEnabled to enable/disable swiping between pages.
Android ViewPager widget is found in the support library and it allows the user to swipe left or right to see an entirely new screen. Today we're implementing a ViewPager by using Views and PagerAdapter. Though we can implement the same using Fragments too, but we'll discuss that in a later tutorial.
Best solution for me. -First, you create a class like this:
public class CustomViewPager extends ViewPager { private Boolean disable = false; public CustomViewPager(Context context) { super(context); } public CustomViewPager(Context context, AttributeSet attrs){ super(context,attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { return !disable && super.onInterceptTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { return !disable && super.onTouchEvent(event); } public void disableScroll(Boolean disable){ //When disable = true not work the scroll and when disble = false work the scroll this.disable = disable; } }
-Then change this in your layout:<android.support.v4.view.ViewPager
for this<com.mypackage.CustomViewPager
-Finally, you can disable it:view_pager.disableScroll(true);
or enable it: view_pager.disableScroll(false);
I hope that this help you :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With