Writing in the front
At the beginning of the project, PM: You don’t need to slide this one, it’s too fancy… Me: now the mainstream APP, generally can slide, so also more in line with user habits… Now, PM: Why can’t you slide this? It doesn’t meet the needs of users. I:…
Anyway, did you write it down, or did you throw it out
Override the ViewPager method
I’m using ViewPager+FragmentPagerAdapter
To disable the sliding effect, it’s pretty easy, just rewrite the ViewPager method.
Define a Boolean value to indicate whether sliding is allowed.
Private Boolean isCanScroll = true;Copy the code
Sets whether sliding is allowed in subclasses that inherit it
public void setCanScroll(boolean canScroll) {
isCanScroll = canScroll;
}
Copy the code
Override ViewPager’s interceptor method, which is an interceptor used for event handling and can change the direction of event delivery, which determines whether Touch events are delivered to the subcontrol.
If false is returned, it’s not intercepted, it’s passed to the child control and if true is returned, it’s intercepted, handled by its own onTouchEvent
Super. OnInterceptTouchEvent (ev), the default is to return true.
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return isCanScroll && super.onInterceptTouchEvent(ev);
}
Copy the code
Override the ViewPager handler that determines whether the current control consumes the event
If false is returned, there is no consumption, and layers are passed up. If true is returned, it is consumed and the event is over.
Super.ontouchevent (ev), which returns false by default.
@Override
public boolean onTouchEvent(MotionEvent ev) {
return isCanScroll && super.onTouchEvent(ev);
}
Copy the code
use
In the Activity that you want to use ViewPager
// Use CustomViewPager to inherit ViewPager private CustomViewPager container;Copy the code
In the onCreate method
Container. SetCanScroll (false);Copy the code
In the layout file.xml
<! Slide - disable page navigation menu -- > < com. Yapping. Common. View. CustomViewPager android: id = "@ + id/containerVP" android: layout_width = "match_parent" android:layout_height="0dp" android:layout_weight="1" />Copy the code
By following the steps above, you can disable the left/right slide effect of the ViewPager.
Ps: Android onInterceptTouchEvent() and onTouchEvent(). Detailed explanation of interception and processing methods
Overwrite file download address