简体   繁体   English

Android-在子视图触摸上忽略父视图触摸

[英]Android - ignore parent view touch on child view touch

I have a layout like 我有这样的布局

RelativeLayout_Parent
  -> RelativeLayout_Child

both views having touch event. 两个视图都有触摸事件。 But when I touch on RelativeLayout_Child , touch event for parent is also fired. 但是当我触摸RelativeLayout_Child ,也会触发父级的touch事件。

How to ignore parent view touch on child view touch? 如何忽略子视图触摸上的父视图触摸?

Very simple, implement OnTouchListener over your child view, and upon receiving Touch Event just return true fro child, this will make sure touch event is not propagated to others. 非常简单,在您的子视图上实现OnTouchListener,并且在收到Touch Event时才返回true来回给孩子,这将确保touch事件不会传播给其他人。

    child_view.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // this will make sure event is not propagated to others, nesting same view area
            return true;
        }

    });

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

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