Source code analysis of the redraw process initiated by View's requestLayout (Android Q)

Source code analysis of the redraw process initiated by View's requestLayout (Android Q)


We usually call the requestLayout method of the View class to update the view, so how does it initiate the redrawing logic?

View's requestLayout method:

    public void requestLayout() {
        ……
        if (mParent != null && !mParent.isLayoutRequested()) {
            //调用父视图的 requestLayout 方法
            mParent.requestLayout();
        }
        ……
    }

This method sets some properties, and then calls the requestLayout method of the parent view. This method will always be called up the view tree, all the way to the root view, which is DecorView. But in the end, is the requestLayout method of DecorView called?

Let's first look at where is mParent assigned?

View's assignParent method

    protected ViewParent mParent;
    void assignParent(ViewParent parent) {
        if (mParent == null) {
            mParent = parent;
        } else if (parent == null) {
            mParent = null;
        } else {
            throw new RuntimeException("view " + this + " being added, but"
                    + " it already has a parent");
        }
    }

mParent is set through the parameters of assignParent, and who called the assignParent method?

The setView method of ViewRootImpl:

Let's go back, the setView method of ViewRootImpl is called when DecorView is to be added to Window.

    public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView){
        ……
        view.assignParent(this);
        ……
    }

In fact, in the setView method of ViewRootImpl, the assignParent method of View is called, and the parameter is the ViewRootImpl object. In other words, mParent of DecorView is a ViewRootImpl object.

Therefore, the requestLayout method of View finally bubbled through its mParent, and finally called the requestLayout method in ViewRootImpl. Isn't this connected to the content of the first half of this article? Both of these view display processes are initiated by the requestLayout method in ViewRootImpl.

to sum up


The requestLayout method of the View class updates the view. How does it initiate the redraw logic?

  1. In the setView method of ViewRootImpl, the assignParent method of View is called, and the parameter is the ViewRootImpl object. In other words, mParent of DecorView is a ViewRootImpl object.

  2. The requestLayout method of View finally bubbled through its mParent, and finally called the requestLayout method in ViewRootImpl. So essentially, it is initiated through the requestLayout method in ViewRootImpl.


PS: For more analysis articles, please check the series of articles -> "Analysis of the underlying principles of Android"
PS: For more analysis articles, please check the series of articles -> "Analysis of the underlying principles of Android"
PS: For more analysis articles, please view the series- > "Analysis of the underlying principles of Android"

Guess you like

Origin blog.csdn.net/u011578734/article/details/110958596