1. 程式人生 > >去除ViewPager介面切換動畫、禁止滑動

去除ViewPager介面切換動畫、禁止滑動

public class CustomViewPager extends ViewPager {


	public CustomViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CustomViewPager(Context context) {
		super(context);
	}
	
	//去除頁面切換時的滑動翻頁效果
		@Override
		public void setCurrentItem(int item, boolean smoothScroll) {
			// TODO Auto-generated method stub
			super.setCurrentItem(item, smoothScroll);
		}

		@Override
		public void setCurrentItem(int item) {
			// TODO Auto-generated method stub
			super.setCurrentItem(item, false);
		}

}

直接繼承即可

加入以下程式碼可以禁止滑動,也可單獨使用

private static final String TAG = "MyViewPager";
	private boolean result = false;

	public CustomViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CustomViewPager(Context context) {
		super(context);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent arg0) {
		if (result)
			return super.onInterceptTouchEvent(arg0);
		else
			return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent arg0) {
		if (result)
			return super.onTouchEvent(arg0);
		else
			return false;
	}
/**
     * 解決viewPager與百度地圖滑動衝突
     * */
    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if(v.getClass().getName().equals("com.baidu.mapapi.map.MapView")) {
            return true;
        }
        //if(v instanceof MapView){
        //    return true;
        //}
        return super.canScroll(v, checkV, dx, x, y);
    }