import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* Created by apple on 17/4/20.
*/
public class CanotSlidingViewpagerextends ViewPager {
/**
* 上一次x坐標
*/
? ? 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;
//-----禁止左滑-------左滑:上一次坐標 > 當前坐標
? ? @Override
? ? public boolean dispatchTouchEvent(MotionEvent ev) {
if(isCanScroll){
return super.dispatchTouchEvent(ev);
? ? ? ? }else {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN://按下如果‘僅’作為‘上次坐標’,不妥,因為可能存在左滑,motionValue大于0的情況(來回滑,只要停止坐標在按下坐標的右邊,左滑仍然能滑過去)
? ? ? ? ? ?beforeX = ev.getX();
break;
? ? ? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
float motionValue = ev.getX() -beforeX;
? ? ? ? ? ? ? ? ? ? if (motionValue >0) {//禁止左滑
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? }
beforeX = ev.getX();//手指移動時,再把當前的坐標作為下一次的‘上次坐標’,解決上述問題
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
break;
? ? ? ? ? ? }
return super.dispatchTouchEvent(ev);
? ? ? ? }
}
public boolean isScrollble() {
return isCanScroll;
? ? }
/**
* 設置 是否可以滑動
? ? * @param isCanScroll
? ? */
? ? public void setScrollble(boolean isCanScroll) {
this.isCanScroll = isCanScroll;
? ? }