由于使用了Github上的开源项目,开始以为需要自己在CustomViewAbove中改写OnInterceptTouchEvent(MotionEvent ev);
参看了一下百度视频App,发现它在首页里的ViewPager控件在下拉后,只有上面部分可以响应左右滑动事件,下面的部分却不可以,于是我猜想它把上面部分写死了,先调用view.getHitRect(rect)来获得viewpager的矩形框位置,然后,判断触摸的点是否在矩形框内。
Rect rect = new Rect();
viewPager.getHitRect(rect);
if(rect.contains(event.getX(), event.getY())
return false;
本以为要自己写的。后来在查看CustomViewAbove的OnInterceptTouchEvent(MotionEvent ev)方法时,看到有个方法thisTouchAllowed(ev);
case MotionEvent.ACTION_DOWN:
mActivePointerId = ev.getAction() & ((Build.VERSION.SDK_INT >= 8) ? MotionEvent.ACTION_POINTER_INDEX_MASK : MotionEvent.ACTION_POINTER_INDEX_MASK);
mLastMotionX = mInitialMotionX = MotionEventCompat.getX(ev, mActivePointerId);
mLastMotionY = MotionEventCompat.getY(ev, mActivePointerId);
if (thisTouchAllowed(ev)) {
mIsBeingDragged = false;
mIsUnableToDrag = false;
if (isMenuOpen() && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
mQuickReturn = true;
}
} else {
mIsUnableToDrag = true;
}
break;
于是展开去看
private boolean thisTouchAllowed(MotionEvent ev) {
int x = (int) (ev.getX() + mScrollX);
if (isMenuOpen()) {
return mViewBehind.menuOpenTouchAllowed(mContent, mCurItem, x);
} else {
switch (mTouchMode) {
case SlidingMenu.TOUCHMODE_FULLSCREEN:
return !isInIgnoredView(ev);
case SlidingMenu.TOUCHMODE_NONE:
return false;
case SlidingMenu.TOUCHMODE_MARGIN:
return mViewBehind.marginTouchAllowed(mContent, x);
}
}
return false;
}
居然后有个方法,isInIgnoreView(),如果不在忽略的View中,就会返回true传给CustomViewAbove自己的onTouchEvent()来消费该事件,于是猜想是不是有个方法可以可以把slidingMenu把想忽视侧滑这一触摸事件的View加入判断中,
private boolean isInIgnoredView(MotionEvent ev) { Rect rect = new Rect(); for (View v : mIgnoredViews) { v.getHitRect(rect); if (rect.contains((int) ev.getX(), (int) ev.getY())) return true; } return false; }
public void addIgnoredView(View v) { if (!mIgnoredViews.contains(v)) { mIgnoredViews.add(v); } }SlidingMenu类中又调用了CustomViewAbove的addIgnoreView()
/** * Add a View ignored by the Touch Down event when mode is Fullscreen * * @param v * a view to be ignored */ public void addIgnoredView(View v) { mViewAbove.addIgnoredView(v); }
public static SlidingMenu mSlidingMenu = getSlidingMenu();
ActMain.mSlidingMenu.addIgnoredView(viewPager);
大功告成!