【重写ViewPager,禁止其左右滑动】自定义ViewPager,重写其滑动的事件,即可控制其是否可以滑动.
package com.xspacing.aiqiyi.custom;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* @ClassName CustomViewPager.java
* @Description 禁止ViewPager左右滑动
* @author Smile
* @version v1.0
* @date 2016年9月20日
*/
public class CustomViewPager extends ViewPager {private boolean isNoScoll = true;
public CustomViewPager(Context context) {
super(context);
}public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (isNoScoll) {
return false;
}
return super.onInterceptTouchEvent(arg0);
}@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent arg0) {
if (isNoScoll) {
return false;
}
return super.onTouchEvent(arg0);
}
//外部设置是否可滑动方法
public void setNoScoll(boolean isNoScoll) {
this.isNoScoll = isNoScoll;
}}