解决SlidingMenu滑动Menu无法关闭菜单的问题

按需求使用到SlidingMenu做侧栏菜单,但有个缺陷就是滑动菜单区域无法像滑动菜单以外的区域那样可以关闭菜单,通过看国外网友的解决方案,解决了这个问题,感谢他们,在此做个日记。


1、修改 CustomViewBehind.java

    @Override
   public boolean onInterceptTouchEvent(MotionEvent e) {
   	return !mChildrenEnabled;
   }

   @Override
   public boolean onTouchEvent(MotionEvent e) {
    	return !mChildrenEnabled;
   }

修改为:

    @Override
   public boolean onInterceptTouchEvent(MotionEvent e) {
      return mViewAbove.onInterceptTouchEvent(e);
   }

   @Override
   public boolean onTouchEvent(MotionEvent e) {
      return mViewAbove.onTouchEvent(e);
   }

2、修改 CustomViewAbove.java的onTouchEvent方法

case MotionEvent.ACTION_DOWN:
   /*
    * If being flinged and user touches, stop the fling. isFinished
    * will be false if being flinged.
    */
   completeScroll();

   // Remember where the motion event started
   int index = MotionEventCompat.getActionIndex(ev);
   mActivePointerId = MotionEventCompat.getPointerId(ev, index);
   mLastMotionX = mInitialMotionX = ev.getX();
   break;
修改为

case MotionEvent.ACTION_DOWN:
   int index = MotionEventCompat.getActionIndex(ev);
   mActivePointerId = MotionEventCompat.getPointerId(ev, index);
   if (mActivePointerId == INVALID_POINTER)
      break;
   mLastMotionX = mInitialMotionX = MotionEventCompat.getX(ev, index);
   mLastMotionY = MotionEventCompat.getY(ev, index);
   if (thisTouchAllowed(ev)) {
      mIsBeingDragged = false;
      mIsUnableToDrag = false;
      if (isMenuOpen() && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
         mQuickReturn = true;
      }
   } else {
      mIsUnableToDrag = true;
   }
   return mQuickReturn;


设置SlidingMenu

mSlidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);


你可能感兴趣的:(Android自定义控件)