简体   繁体   English

在父视图中处理触摸,但在子视图中处理单击事件

[英]Handle touch in parent view but Handle Click event in child

i have a custom ViewGroup that extends linearLayout and this view group has a gridView as child . 我有一个自定义的ViewGroup,它扩展了linearLayout,这个视图组有一个gridView作为child。 i want if mGridView clicked mGridView.onClickListener called and executed but handle other touch events(like move mGridView ) in my custom view Group (onTouchEvent() executed ). 我想如果mGridView单击并执行了mGridView.onClickListener,但是要在我的自定义视图组(执行onTouchEvent())中处理其他触摸事件(如移动mGridView)。 so i ovveride onInterceptTouchEvent : 所以我ovveride onInterceptTouchEvent:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    int action = ev.getAction();
    int y = (int) ev.getY();

    if( action == MotionEvent.ACTION_DOWN && y > mGridView.getTop() )  //y > mGridView,getTop() checks if position of touch in mGridView Position or not.
       return false;

    return true;

}

but mGridView.OnClickListener() never called . 但是mGridView.OnClickListener()从未调用过。

A click event is a composite event, it does not consist only of an MotionEvent.ACTION_DOWN. 点击事件是一个复合事件,它不仅包含MotionEvent.ACTION_DOWN。

It should have a MotionEvent.ACTION_DOWN, probably several other MotionEvent.ACTION_MOVE and finally a MotionEvent.ACTION_UP, and all these inside the bounds of your clicked view. 它应该有一个MotionEvent.ACTION_DOWN,可能还有其他几个MotionEvent.ACTION_MOVE,最后还有一个MotionEvent.ACTION_UP,所有这些都在您单击的视图的范围内。

You should define other MotionEvents to "return false" inside the method onInterceptTouchEvent(MotionEvent ev). 您应该在onInterceptTouchEvent(MotionEvent ev)方法内定义其他MotionEvent,以“返回false”。 maybe something like: 也许像这样:

if( (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_DOWNaction == MotionEvent.ACTION_MOVE || action == MotionEvent.ACTION_UP) && y > mGridView.getTop() )

Appart from this which you have probably already read, take a look at Detecting Common Gestures APPART从这个你可能已经读,看看检测常见的手势

Alternatively(but not really suggesting it) try not using an OnClickListener on your View but an OnTouchListener. 另外(但没有真正建议),请尝试在您的View上不使用OnClickListener,而在OnTouchListener上使用。 With your current code you should receive there the MotionEvent.ACTION_DOWN you are now returning false 使用当前代码,您应该在那里收到MotionEvent.ACTION_DOWN,现在返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM