1. 程式人生 > >仿淘寶的商品詳情拖動,ViewPager和ScrollView WebView的滑動衝突

仿淘寶的商品詳情拖動,ViewPager和ScrollView WebView的滑動衝突

仿淘寶的商品詳情拖動遇到的問題。 使用結構就是DirectionalViewPager 嵌套了(1)ScrollView (2)WebView 輸入圖片說明

情況: 豎直方向的ViewPager DirectionalViewPager 嵌套了(1)ScrollView (2)WebView 出現滑動衝突

實現目標: (1)在ScrollView滑動到底部時,繼續下拉觸發DirectionalViewPager的滑動切換 (2)在WebView 滑動到頂端時,繼續上拉觸發DirectionalViewPager的滑動切換

利用void android.view.ViewGroup.requestDisallowInterceptTouchEvent(boolean disallowIntercept)方法 在子控制元件上通知外層的ViewPager進行事件攔截和 不攔截

要點1 判斷ScrollView在底部

int scrollY = svInfo.getScrollY(); int height = svInfo.getHeight(); int scrollViewMeasuredHeight = svInfo.getChildAt(0) .getMeasuredHeight();

if ((scrollY + height) == scrollViewMeasuredHeight) { //在底部 }

要點2 判斷WebView在頂部

if (mWebView.getScrollY() == 0) { // 在webview的頂部 }

要點3 通過手勢去判斷方向

gestureLisenter = new SimpleOnGestureListener() {
			@Override
			public boolean onScroll(MotionEvent e1, MotionEvent e2,
					float distanceX, float distanceY) {
			
CommUtil.logD(TAG, "onScroll");
				try {
					float x = e2.getX() - e1.getX();
					float y = e2.getY() - e1.getY();
					float y_limit = 5;
					float y_abs = Math.abs(y);
					float x_abs = Math.abs(x);
					if (mWebView.getScrollY() == 0) {
						// 在webview的頂部
						if (x_abs < y_abs) {
							if (y > y_limit || y < -y_limit) {
								if (y > 0) {
									// 下滑
									CommUtil.logD(TAG, "in WebView top, onScroll up");
									mActivity
											.getViewPager()
											.requestDisallowInterceptTouchEvent(
													false);
									return true;
								}
							}
						}
					}
				} catch (Exception e) {
					CommUtil.logE(TAG, e.toString());
				}
				return false;
}
		};
		mGesture = new GestureDetector(gestureLisenter);

	mWebView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
CommUtil.logD(TAG, "onTouch");
				if(mGesture.onTouchEvent(event)) {
					return true;
				}
				mActivity.getViewPager().requestDisallowInterceptTouchEvent(
						true);
				return false;
​			}
			
		});

————————————————————————————————————————————————————

gestureLisenter = new SimpleOnGestureListener() {
			@Override
			public boolean onScroll(MotionEvent e1, MotionEvent e2,
					float distanceX, float distanceY) {
				
try {
					float x = e2.getX() - e1.getX();
					float y = e2.getY() - e1.getY();
					float y_limit = 5;
					float y_abs = Math.abs(y);
					float x_abs = Math.abs(x);
					int scrollY = svInfo.getScrollY();
					int height = svInfo.getHeight();
					int scrollViewMeasuredHeight = svInfo.getChildAt(0)
							.getMeasuredHeight();
					if ((scrollY + height) == scrollViewMeasuredHeight) {
						//在底部
						if (x_abs < y_abs) {
							if (y > y_limit || y < -y_limit) {
								if (y <= 0) {
									// 上滑
									CommUtil.logD(TAG, "in ScrollView  bottom, onScroll up");
									mActivity.getViewPager()
											.requestDisallowInterceptTouchEvent(
													false);
									return true;
								}
							}
						}
					}
				} catch (Exception e) {
					CommUtil.logE(TAG, e.toString());
				}
				return false;
}
		};
		mGesture = new GestureDetector(gestureLisenter);


	svInfo.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
			
CommUtil.logD(TAG, "onTouch");
				if( mGesture.onTouchEvent(event)) {
					return true;
				} 
				mActivity.getViewPager().requestDisallowInterceptTouchEvent(
						true);
				return false;
}

		});