Android has a sliding event conflict

First of all, we assume such a scenario: a ViewPager is nested inside a ViewPager, and the inner sliding direction is the same as the outer sliding direction. How to resolve this conflict?
There are two solutions for sliding conflict: external interception method and internal interception method.

External interception

Scenario: A ViewPager nests a Listview, one slides left and right, and the other slides up and down. At this time, we can use the external interception method to handle the conflict. In the parent container ViewPager, override the onInterceptTouchEvent() method to determine that the event will be intercepted when sliding left and right, and not intercepting when sliding up and down, and the event will be handled by the child element Listview. First we need to rewrite a ViewPager, called MyViewPager, and then rewrite the onInterceptTouchEvent() method. The specific code is as follows:

package com.example.zhiyu.util;

import android.content.Context;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

class MyViewPager extends ViewPager {
    
    
    private int startX;
    private int startY;
    public MyViewPager(Context context) {
    
    
        super(context);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    
    
        switch (ev.getAction())
        {
    
    
            case MotionEvent.ACTION_DOWN:
                startX= (int) ev.getX();
                startY= (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:

                int dX= (int) (ev.getX()-startX);
                int dY= (int) (ev.getY()-startX);
                if(Math.abs(dX)>Math.abs(dY)){
    
    //左右滑动
                    return true;
                }else {
    
    //上下滑动
                    return false;
                }
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}

In this way, the sliding conflict in this case is solved. The program is shown in the following figure: The
Insert picture description here
above code is the typical logic of external interception. You only need to rewrite the onInterceptTouchEvent() method to modify the event currently needed by the parent container.

Internal interception

Scenario: A ViewPager is nested with a ViewPager, and both of them slide left and right. At this time, we can use the internal interception method to handle the conflict. That is, to override the dispatchTouchEvent() method of the child element and call getParent().requestDisallowInterceptTouchEvent(true) is that the parent container cannot intercept the event required by the child element. Let's look at the specific code:

public boolean dispatchTouchEvent(MotionEvent event) {
    
    
          ...
  
          switch (action) {
    
    
              case MotionEvent.ACTION_MOVE:
                          getParent().requestDisallowInterceptTouchEvent(true);
  
                  break;
              case MotionEvent.ACTION_MOVE:
                 if(子元素需要处理此事件)
                             getParent().requestDisallowInterceptTouchEvent(true);
 
                 break;
             case MotionEvent.ACTION_UP: {
    
    
                 break;
         }
         ...
         return super.dispatchTouchEvent(event);
     }

Of course, you also need to modify the onInterceptTouchEvent() method of the parent container. The code is as follows:

	  Override
      public boolean onInterceptTouchEvent(MotionEvent ev) {
    
    
  
              int action=ev.getAction();
              if(action==MotionEvent.ACTION_DOWN){
    
    
                  return false;
              }else {
    
    
                  return true;
              }
         }

The above are two solutions to resolve sliding conflicts.

Guess you like

Origin blog.csdn.net/haohaiwangji/article/details/107411923