Can't get touch event detected on a view and click event detected on a parent view

混江龙づ霸主 提交于 2019-12-08 08:37:35

问题


I've got a layout 'layout_base' that fills the screen and a child view 'home'. I set an OnClickListener on the 'layout_base' to do some action (close a popup if it's open), and an OnTouchListener on 'home' to do some other action (translate on fling).

My issue is that I can only get the action done if I return true in the onTouch method of the 'home' touch listener. If I return false, onFling is not called. Why? From the doc I've read that this only mean that the event is consumed, i.e. from my understanding that it is not propagated to other listener.

So:

  • if I return true in onTouch, onFling is called, but then the onClick in the onClickListener of the parent view 'layout_base' is not called.

  • if I return false in onTouch, onFling is not called, but then the onClick in the onClickListener of the parent view 'layout_base' is called.

How can I get both my onFling detection working on 'home', and my onClick detection working on 'layout_base'?

Thanks you

layout_base = findViewById(R.id.layout_base);
layout_base.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) { 
        closePopup();  // never called
    }
});     

contentGestureDetector = new GestureDetector(new ContentGestureListener());
contentGestureListener = new View.OnTouchListener() 
{
    public boolean onTouch(View v, MotionEvent event) 
    {  
        contentGestureDetector.onTouchEvent(event);
        return true; // onFling only called if return true
    }
};

home.setOnTouchListener(contentGestureListener);

class ContentGestureListener extends SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {


        final int SWIPE_MIN_DISTANCE = 120;
        final int SWIPE_MAX_OFF_PATH = 250;
        final int SWIPE_THRESHOLD_VELOCITY = 200; 

        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;

        if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            if(!mMenuOpen){                        
                mOpenMenuAnim.start();
                mMenuOpen = true;
            }
        }
        else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            if(mMenuOpen){ 
                mCloseMenuAnim.start();
                mMenuOpen = false;
            }
        }

        return false;
    }
}

回答1:


the onTouch() consume the event , you should in your method onTouch() , return false instead of true

public boolean onTouch(View v , MotionEvent motionEvent) {
  // your code 
  return false;
}

EDIT :

change your method onTouch() like this :

public boolean onTouch(View v, MotionEvent event) 
    {  
        return contentGestureDetector.onTouchEvent(event);
    }



回答2:


I have run accross the same thing. You need to return true in the onTouch down event to receive move and up events (and make gestures work). Then you need to implement one of the functions talked about here to make the rest work. Hope this helps.




回答3:


I would try to

  • return false in onTouch => onClick gets called
  • in Home.onInterceptTouchEvent, you can return true if you detect a 'fling' (by comparing the x/y values of the incoming MOVE action)

By returning true, the events towards the layout_base will be canceled and no onClick will occur there in that case.



来源:https://stackoverflow.com/questions/9991450/cant-get-touch-event-detected-on-a-view-and-click-event-detected-on-a-parent-vi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!