[Source code] Communication from CoordinatorLayout to collection

CoordinatorLayout is a very old thing, but I just heard that I am a programmer. Take a look at the source code and ease up.
A ViewGroup has no design ideas to speak of. It is nothing more than: using the Mediator mode to solve the random communication problem of sub-elements in the collection (DependOn), using the Decor mode to solve the problem of unnecessary inheritance (the agent gives a lot of View's onXXX method). Such high-level thinking.

Reasonable design

  • For dependencies, they must all form DAGs, and there is a DirectedAcyclicGraph class that specializes in this.
    DirectedAcyclicGraph in the design package

detail

  • All touch events are passed to Behavior first in order, here are all Views . The receiving order of View is no longer the link of traditional event delivery, but:
    • isChildrenDrawingOrderEnabled == true时,getChildDrawingOrder
    • The order in which views are added
  • ViewChanged is triggered during PreDraw, which is actually quite a coincidence. Because all changes will lead to Draw, then preDraw can easily hook all changes
  • DirectedAcyclicGraph uses an adjacency list to represent graphs
  • DFS traversal is equivalent to post-order traversal of the tree, the root is printed last

think

So can all these kinds of problems be done by sticking one (multiple) Behaviors on child elements, such as nested Fragments? In my opinion, not.
ViewGroup can do this because:

  • The state changes of all child Views can be automatically reflected to a hook (PreDraw). Any multi-point hook that needs to be triggered manually is easy to cause problems, and it places very high requirements on the implementation of sub-elements
  • The interface of the sub-view is very rich, and in more cases, especially the Fragment, it is difficult to give a reasonable external interface, which makes it difficult to use. Of course, in the various examples of CoordinatorLayout, some things are also done through type coercion

That is to say, if and only if the child element does not need to manually trigger an event and the communication between the child elements is very uniform, it is suitable to directly apply the idea of ​​CoordinatorLayout.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325837015&siteId=291194637