public class NoScrollViewPager extends ViewPager { private boolean noScroll = true; //true 表明不能滑動 //false 表明能滑動 //主佈局:用viewpager點noScroll而後,記得複製這個佈局的路徑而後粘貼到佈局裏的viewpager,把實例化的名字改成NoScrollViewPager該類的名字。 public NoScrollViewPager(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public NoScrollViewPager(Context context) { super(context); } public void setNoScroll(boolean noScroll) { this.noScroll = noScroll; } @Override public void scrollTo(int x, int y) { super.scrollTo(x, y); } @Override public boolean onTouchEvent(MotionEvent arg0) { /* return false;//super.onTouchEvent(arg0); */ if (noScroll) return false; else return super.onTouchEvent(arg0); } @Override public boolean onInterceptTouchEvent(MotionEvent arg0) { if (noScroll) return false; else return super.onInterceptTouchEvent(arg0); } @Override public void setCurrentItem(int item, boolean smoothScroll) { super.setCurrentItem(item, smoothScroll); } @Override public void setCurrentItem(int item) { super.setCurrentItem(item, false);//表示切換的時候,不須要切換時間。 } }