onInterceptTouchEvent, onTouchEvent only see ACTION_DOWN

后端 未结 4 1140
春和景丽
春和景丽 2021-01-18 11:56

I have a top level ViewGroup, which I call SliderView, in which I want to detect swiping. This is mostly working, but one weird failure persists.

The essence of Sli

相关标签:
4条回答
  • 2021-01-18 12:13

    From Android developer's reference (http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)):

    "2. .... Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal."

    Maybe because your onTouchEvent always returns true..?

    0 讨论(0)
  • 2021-01-18 12:19

    Due to the answer of user123321 here

    onInterceptTouchEvent only get called if the parent has a child view which returns "true" from onTouchEvent. Once the child returns true, the parent now has a chance to intercept that event

    0 讨论(0)
  • 2021-01-18 12:24

    All you need is to call

    requestDisallowInterceptTouchEvent(true);
    

    on the parent view, like this -

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch(motionEvent.getActio){
                }
    
                return false; 
    
             }
    
    0 讨论(0)
  • 2021-01-18 12:35

    When intercepting onTouchEvent, there are two things to do to properly intercept the touches (all else being default).

    1. Return false in onInterceptTouchEvent()

      @Override
      public boolean onInterceptTouchEvent(MotionEvent me) {
          return false;
      }
      
    2. Return true in onTouchEvent()

      @Override
      public boolean onTouchEvent(MotionEvent me) {
      
          switch (me.getAction()) {
          case MotionEvent.ACTION_DOWN:
              log("MotionEvent.ACTION_DONE");
              break;
          case MotionEvent.ACTION_MOVE:
              log("MotionEvent.ACTION_MOVE");
              break;
          case MotionEvent.ACTION_CANCEL:
              log("MotionEvent.ACTION_CANCEL");
              userActionDown = false;
              break;
          case MotionEvent.ACTION_UP:
              log("MotionEvent.ACTION_UP");
              break;
          }
      
          return true;
      }
      
    3. Then, for your case (and others). Do all your calculations in the onTouchEvent() as shown above. The onInterceptTouchEvent() will only be called once for the ACTION_DOWN. But, the onTouchEvent will also get the ACTION_DOWN event, and you'll need to return true there, rather than the super.

    For more information regarding onInterceptTouchEvent(): http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)

    ps - When you ask questions here, you should also write the description of what you are trying to do. You might quite possibly find much better ways of doing things. For your case of navigation, the real answer you are looking for is ViewPager. It works great and is very easy to implement. You should also check out some other easy navigation patters that Android has to offer developers: link.

    0 讨论(0)
提交回复
热议问题