无法获得在查看检测到触摸事件和点击事件在父视图检测 [英] Can't get touch event detected on a view and click event detected on a parent view

查看:170
本文介绍了无法获得在查看检测到触摸事件和点击事件在父视图检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局layout_base,填补了屏幕和一个子视图的家。
我设置了 OnClickListener 在'layout_base做一些动作(关闭弹出,如果它是开放的),以及 OnTouchListener 在'家',做一些其他操作(翻译上一扔)。

我的问题是,我只能拿到动作做,如果我回到真正中的的 onTouch 方法'家'触摸监听器。如果我回到,onFling不叫。为什么?从文档我读过,这仅意味着该事件被消耗,即,从我的理解,它不传播到其它监听器。

所以:


  • 如果我回到真正 onTouch onFling 被调用,但在父视图的onClickListener onClick的layout_base'不叫。


  • 如果我回到 onTouch onFling 不叫,但随后的onClick父视图的onClickListenerlayout_base之称。


如何得到我的两个onFling检测的家,工作和我的onClick检测上layout_base工作?

感谢您

  layout_base = findViewById(R.id.layout_base);
layout_base.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图v){
        closePopup(); //从来没有所谓的
    }
});contentGestureDetector =新GestureDetector(新ContentGestureListener());
contentGestureListener =新View.OnTouchListener()
{
    公共布尔onTouch(视图V,MotionEvent事件)
    {
        contentGestureDetector.onTouchEvent(事件);
        返回true; // onFling只有当叫真正的回报
    }
};home.setOnTouchListener(contentGestureListener);类ContentGestureListener扩展SimpleOnGestureListener {    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
        最终诠释SWIPE_MIN_DISTANCE = 120;
        最终诠释SWIPE_MAX_OFF_PATH = 250;
        最终诠释SWIPE_THRESHOLD_VELOCITY = 200;        如果(Math.abs(e1.getY() - e2.getY())> SWIPE_MAX_OFF_PATH)
            返回false;        如果(e2.getX() - e1.getX()> SWIPE_MIN_DISTANCE&放大器;&放大器; Math.abs(velocityX)GT; SWIPE_THRESHOLD_VELOCITY){
            如果(!mMenuOpen){
                mOpenMenuAnim.start();
                mMenuOpen = TRUE;
            }
        }
        否则如果(e1.getX() - e2.getX()> SWIPE_MIN_DISTANCE&放大器;&放大器; Math.abs(velocityX)GT; SWIPE_THRESHOLD_VELOCITY){
            如果(mMenuOpen){
                mCloseMenuAnim.start();
                mMenuOpen = FALSE;
            }
        }        返回false;
    }
}


解决方案

onTouch()消耗你的方法时,你应该 onTouch (),返回而不是真正的

 公共布尔onTouch(视图V,MotionEvent motionEvent){
  //你的code
  返回false;
}

编辑:

改变你的方法onTouch()是这样的:

 公共布尔onTouch(视图V,MotionEvent事件)
    {
        返回contentGestureDetector.onTouchEvent(事件);
    }

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;
    }
}

解决方案

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);
    }

这篇关于无法获得在查看检测到触摸事件和点击事件在父视图检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆