because the project needs, to do a sliding month selection of the effect, and sliding to the current month will be forbidden to slide left to the next one months, Baidu, mostly is forbidden to the left and right two-way sliding, to meet the effect, continue to Baidu understand the next touch event distribution, I decided to write a try, the results are very good . began to appear a bug, sliding back and forth can still slide the past, the results found that only when pressed as the last coordinate, and the current coordinates, there will definitely be an unsatisfied situation. This problem has been solved, look directly at the code bar.
Package Com.example.xxx;import Android.content.context;import Android.support.v4.view.viewpager;import Android.util.attributeset;import android.view.motionevent;/** * Can be set to prevent sliding of the viewpager (One-way prohibition: swipe left) * Core method: Setscrollble () * @author Alan * */public class Canotslidingviewpager extends Viewpager {/** * last x-coordinate */private float Beforex; Public Canotslidingviewpager (context context, AttributeSet Attrs) {Super (context, attrs); TODO auto-generated Constructor stub} public Canotslidingviewpager (context context) {super (context); } Private Boolean iscanscroll = true; ----------disable left and right swipe------------------//@Override//public boolean ontouchevent (motionevent ev) {//if (Iscans Croll) {//return super.ontouchevent (EV),//} else {//return false;//}////}/// /@Override//public boolean onintercepttouchevent (Motionevent arg0) {////TODO auto-generated method stub// if (Iscanscroll){//return super.onintercepttouchevent (ARG0);/} else {//return false;//}////} -------------------------------------------//-----Disable left slide-------left: Previous coordinates > current coordinates @Override publi C Boolean dispatchtouchevent (Motionevent ev) {if (Iscanscroll) {return super.dispatchtouchevent (EV); }else {switch (ev.getaction ()) {case motionevent.action_down://pressed if ' only ' as ' Last coordinates ', improper, because there may be left slip, Motionvalue greater than 0 case (sliding back and forth, as long as the stop coordinates at the right of the press coordinates, left slide can still slip past) Beforex = Ev.getx (); Break;case motionevent.action _move:float Motionvalue = Ev.getx ()-Beforex;if (Motionvalue < 0) {//disable left slip return true;} Beforex = Ev.getx ();//When the finger moves, the current coordinates are used as the next ' last coordinates ' to solve the above problem break;default:break;} return super.dispatchtouchevent (EV);} } public boolean isscrollble () {return iscanscroll; }/** * Set whether you can swipe * @param iscanscroll */public void setscrollble (Boolean iscanscroll) {this.is CanScroll = Iscanscroll; }}
Right slide to change the Motionvalue judgment to be greater than 0
<pre name= "code" class= "Java" >if (Motionvalue > 0)
Android-viewpager No left slide (right slide)