Android屏蔽/禁止ViewPager左右滑动/滚动
Android原生的ViewPager自带左右滚动,滑动效果,切换ViewPager中的内容。但有时候,可能产品或设计不要它左右滚动或者滑动,屏蔽事件即可:
import android.content.Context;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.view.MotionEvent;public class CustomScrollViewPager extends ViewPager {//是否可以左右滑动?true 可以,像Android原生ViewPager一样。// false 禁止ViewPager左右滑动。private boolean scrollable = false;public CustomScrollViewPager(@NonNull Context context) {super(context);}public CustomScrollViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public void setScrollable(boolean scrollable) {this.scrollable = scrollable;}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {return scrollable;}@Overridepublic boolean onTouchEvent(MotionEvent ev) {return scrollable;}}