adding touch listener for liner layout filled with buttons

前端 未结 2 1668
遇见更好的自我
遇见更好的自我 2020-12-09 06:46

I added a touch event to a linear layout to respond to swipe gestures, and it works well. However, when i add buttons to the layout, the parent liner layout is ignored. How

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 07:49

    You should create your own layout and override onInterceptTouchEvent(MotionEvent ev) method of your layout.

    For Example I created my own layout which extends RelativeLayout

           @Override
           public boolean onInterceptTouchEvent(MotionEvent ev) {
             return true; // With this i tell my layout to consume all the touch events from its childs
          }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            // Log.d(TAG, String.format("ACTION_DOWN | x:%s y:%s", 
                break;
            case MotionEvent.ACTION_MOVE:
            //Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s", 
                break;
            case MotionEvent.ACTION_UP:
                break;
            }
            return true;
        }
    

    And when i put a button to my layout, even i clicked that button, My Layout consumes all the touchEvent because of the onInterceptTouchEvent always returns true.

    Hope this should help you

提交回复
热议问题