Code Monkey home page Code Monkey logo

Comments (8)

GcsSloop avatar GcsSloop commented on August 28, 2024

如果上层View没有默认拦截事件,则事件会首先传递给下层View的,先询问下层View是否需要消费事件。

注册了监听器,只是说明ViewGroup具备事件消费能力,但没有明确表示要拦截事件,所以还是会先询问ChildView,在询问ChildView的时候事件会被最下层的ChildView消费掉,所以ViewGroup不会响应。

from androidnote.

xinlezhao avatar xinlezhao commented on August 28, 2024

既然是viewgroup先拿到事件的为什么没有响应呢? 
难道事件实在回传的时候才响应的吗?

from androidnote.

GcsSloop avatar GcsSloop commented on August 28, 2024

看原理篇函数调用流程,有图。

如果ViewGroup不拦截,那么只有ChildView不消费,ViewGroup才会响应。

from androidnote.

xinlezhao avatar xinlezhao commented on August 28, 2024

明白了,我的理解是这段代码谢谢

public boolean dispatchTouchEvent(MotionEvent ev) {
    boolean result = false;             // 默认状态为没有消费过

    if (!onInterceptTouchEvent(ev)) {   // 如果没有拦截交给子View
        result = child.dispatchTouchEvent(ev);
    }

    if (!result) {                      // 如果事件没有被消费,询问自身onTouchEvent
        result = onTouchEvent(ev);
    }

    return result;
}

from androidnote.

GcsSloop avatar GcsSloop commented on August 28, 2024

嗯,是的。

from androidnote.

zhangyunpenggithub avatar zhangyunpenggithub commented on August 28, 2024

child 会消费掉前提是父控件没有拦截吧????

from androidnote.

jingjc avatar jingjc commented on August 28, 2024

LinearLayout 包含textview,同时注册点击事件,点击textview,LinearLayout的点击事件不打印日志。点击LinearLayout,textview的点击事件不打印日志。

ViewGroup 和 ChildView 同时注册了事件监听器(onClick等),哪个会执行?
事件优先给 ChildView,会被 ChildView消费掉,ViewGroup 不会响应

这个结论是不是应该加上在同一事件中吧,比如是点击textview是一次传动事件的过程,点击LinearLayout是另外一次事件的传递,是不是这样理解?我阅读的时候引起了歧义😭

from androidnote.

wangsanpiao avatar wangsanpiao commented on August 28, 2024

from androidnote.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.