SoFunction
Updated on 2025-04-05

Sliding conflict between Android side slide menu and carousel diagram

When taking over a project, there is a problem that needs to be modified: the carousel cannot slide manually, and manually sliding the carousel will only trigger the side slide menu.

Guess: The touch event of the viewpager control (carousel image) was intercepted by the SlidingMenu control (side slide menu, not a third-party project, customized by the previous developer).

Based on this guess, I customize a ViewPager, rewrite dispatchTouchEvent, onInterceptTouchEvent and onTouchEvent, and print the log in these three methods respectively;

Rewrite the dispatchTouchEvent, onInterceptTouchEvent and onTouchEvent of SlidingMenu, and print the log.

Recompile and run, and play the diagram with the hand pulley, the log is as follows:

06-08 09:52:08.394 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:0
06-08 09:52:08.395 19424-19424/ E/SlidingMenu: onInterceptTouchEvent ev:0
06-08 09:52:08.395 19424-19424/ E/RollViewPager: dispatchTouchEvent ev:0
06-08 09:52:08.395 19424-19424/ E/RollViewPager: onInterceptTouchEvent ev:0
06-08 09:52:08.441 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.441 19424-19424/ E/SlidingMenu: onInterceptTouchEvent ev:2
06-08 09:52:08.442 19424-19424/ E/SlidingMenu: ACTION_MOVE dx:15.473999
06-08 09:52:08.442 19424-19424/ E/RollViewPager: ACTION_MOVE getCurrentItem():1
06-08 09:52:08.442 19424-19424/ E/RollViewPager: dispatchTouchEvent ev:2
06-08 09:52:08.442 19424-19424/ E/RollViewPager: onInterceptTouchEvent ev:2
06-08 09:52:08.459 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.459 19424-19424/ E/SlidingMenu: onInterceptTouchEvent ev:2
06-08 09:52:08.459 19424-19424/ E/RollViewPager: dispatchTouchEvent ev:3
06-08 09:52:08.459 19424-19424/ E/RollViewPager: onInterceptTouchEvent ev:3
06-08 09:52:08.477 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.477 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.495 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.495 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.515 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.515 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.533 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.533 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.551 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.551 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.574 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.574 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.594 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.595 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.611 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.612 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.622 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:2
06-08 09:52:08.622 19424-19424/ E/SlidingMenu: onTouchEvent ev:2
06-08 09:52:08.623 19424-19424/ E/SlidingMenu: dispatchTouchEvent ev:1

As can be seen from the log, the sliding event can be passed to the ViewPager at the beginning, and then it is intercepted by SlidingMenu. This log confirms that this conjecture is correct.

It’s easy to solve the problem after knowing the reason. What I need to consider now is: what effect to achieve.

Expected effect: The carousel can be slided normally. When the carousel is in the first picture, it can slide to the side slide menu.

Someone has shared similar questions online. I've also learned from it here.

Let’s first realize the first function: it can slide the carousel picture normally.

You can set a global variable:public static boolean mRollViewPagerTouching;

Used to indicate whether the carousel is touching.

In the custom ViewPager, determine whether to touch the carousel image

public boolean dispatchTouchEvent(MotionEvent ev) {
    switch (()) {
      case MotionEvent.ACTION_MOVE:
        break;
      case MotionEvent.ACTION_DOWN:
         = true;
        break;
      case MotionEvent.ACTION_UP:
         = false;
        break;
    }
    return (ev);
  }

In SlidingMenu, the following processing is done

public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (){
      return false;
    }
    return (ev);
  }

Recompile and run, the carousel can be slided normally, but when the carousel is in the first picture, it cannot slide to the side slide menu.

Now let’s do the second function: when the carousel is in the first picture, you can slide to the side slide menu.

There are two things to note here: 1. When the carousel picture is in the first picture; 2. Since my side slide menu is on the left, I need to slide my finger to the right to enter the side slide menu, so the second condition should be to swipe right.

Based on the above, modify the custom ViewPager code

public boolean dispatchTouchEvent(MotionEvent ev) {
    // Go back to the starting coordinate when triggered    float x = ();
    switch (()) {
      case MotionEvent.ACTION_MOVE:
        //Get the distance difference        float dx = x - downX;
        //Prevent it from being pressed and judged        if ((dx) > 8) {
          //Judge direction by distance difference          if (dx > 0) {
            // "Right";            if (getCurrentItem() == 0) {
               = false;
            } else {
               = true;
            }
          } else {
            //                "Left";             = true;
          }
        }
        break;
      case MotionEvent.ACTION_DOWN:
        //Storage the coordinates when pressed        downX = x;
         = true;
        break;
      case MotionEvent.ACTION_UP:
         = false;
        break;
    }
    return (ev);
  }

Compile and run again and successfully achieve the expected results.