检测requestlayout in layout问题

前言

相信大家也遇到过类似的问题,比如TextView文字显示不全,view没有按期望的显示或者隐藏,页面没有按期望的刷新,而且这些bug都在4.3以下必现,4.3以上系统就没问题了。这往往是requestLayout in layout问题,这类问题往往都需要花比较久的时间来定位解决,我最近碰到了几次,感觉特别浪费时间,很难找到问题的关键代码,所以准备写一个工具来解决检测这种问题,打印出问题代码的堆栈。
本文代码github地址:https://github.com/chefish/RequestLayoutInLayout

建立demo

A1 request in A3 layout

activity:DemoActivity1
第一步,建立demo,复现问题。
参考requestLayout in layout问题,我建立了如下一个layout,如下所示

<?xml version="1.0" encoding="utf-8"?>
<com.fish.requestlayoutinlayout.view.ALinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.fish.requestlayoutinlayout.MainActivity">

    <com.fish.requestlayoutinlayout.view.AFrameLayout3
        android:id="@+id/a3"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.fish.requestlayoutinlayout.view.AFrameLayout2
            android:id="@+id/a2"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.fish.requestlayoutinlayout.view.ALinearLayout1
                android:id="@+id/a1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <com.fish.requestlayoutinlayout.view.ATextView0
                    android:layout_marginTop="30dp"
                    android:id="@+id/a0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="hello" />
            </com.fish.requestlayoutinlayout.view.ALinearLayout1>
        </com.fish.requestlayoutinlayout.view.AFrameLayout2>

    </com.fish.requestlayoutinlayout.view.AFrameLayout3>


</com.fish.requestlayoutinlayout.view.ALinearLayout>

然后在AFrameLayout3里设置如下layout,在AFrameLayout3的onLayout里调用aFrameLayout1的requestlayout。

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int w = aFrameLayout1.getWidth();
        if (w == ScreenUtil.screenWidth) {
            //call A1 requestlayout
            aFrameLayout1.setLayoutParams(new FrameLayout.LayoutParams(400, -1));
        }
        LogUtil.d("w " + w);
    }

按照上文的推断,layout完毕之后,处于TIME2状态,A1,A2的PFLAG_FORCE_LAYOUT还是1。但是事实却不一样,我发现,layout完毕之后A1,A2的PFLAG_FORCE_LAYOUT变为了0,好像外面有只手把错误给抹掉了。这是为什么呢?用一个onGlobalLayout来检测一下,日志如下,发现的确有一瞬间A1,A2位true了,但是之后遇到了一次forcelayout和onLayout导致A1,A2的标志位变为false。

D/DemoActivity1: @onGlobalLayout A1: true A2: true
D/logfish: AFrameLayout3@forceLayout
D/logfish: AFrameLayout2@onLayout
D/DemoActivity1: @onGlobalLayout A1: false A2: false

原来activity起来的时候,ViewRootImpl会收到一个消息 MSG_RESIZED_REPORT,收到这个消息后,会forceLayout、requestLayout,这里的mView是DecorView,forceLayout一个view会把自己个所有子孙view的 PFLAG_FORCE_LAYOUT 都置位1。

//ViewRootImpl
                    if (mView != null) {
                        forceLayout(mView);
                    }

                    requestLayout();

forceLayout的代码如下所示

//ViewRootImpl
    private static void forceLayout(View view) {
        view.forceLayout();
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            final int count = group.getChildCount();
            for (int i = 0; i < count; i++) {
                forceLayout(group.getChildAt(i));
            }
        }
    }

所以在此时,所有的view的 PFLAG_FORCE_LAYOUT 都置位了1,我们刚才设计的让A1,A2位1,其他为0的想法就被打破了,之后在后面的requestLayout后触发了全局的重新layout,然后所有的PFLAG_FORCE_LAYOUT都被置位了0。我们精心设计的场景就被MSG_RESIZED_REPORT给破坏掉了。

设计改进

activity:DemoActivity2
我们注意到刚才是在activity起来的时候,MSG_RESIZED_REPORT消息触发,然后触发全局大layout,所以我们设计失效。我们只要避开这个环节就好了。我们加一个textview叫switch作为开关位,然后改造一下AFrameLayout3得到 AFrameLayout3_V2,代码如下

//AFrameLayout3_V2
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int w = aFrameLayout1.getWidth();
        if (w == ScreenUtil.screenWidth && Switch.on) {
            //call A1 requestlayout
            aFrameLayout1.setLayoutParams(new LayoutParams(400, -1));
        }
    }

DemoActivity2内新加的switch这个TextView的代码如下

//DemoActivity2
        textViewSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Switch.change();
                textViewSwitch.setText("swtich " + Switch.on);
            }
        });

ok,此时,我们点击switch按钮,发现日志如下

D/DemoActivity2: @onGlobalLayout A1: false A2: false
D/logfish: AFrameLayout3@forceLayout
D/logfish: AFrameLayout2@onLayout
D/DemoActivity2: @onGlobalLayout A1: false A2: false
D/logfish: @requestLayout start com.fish.requestlayoutinlayout.view.ATextView1{531e3f14 V.ED..C. ...P.... 
D/logfish: @requestLayout start com.fish.requestlayoutinlayout.view.AFrameLayout3_V2{531d5098 V.E..... ......I. 
D/logfish: AFrameLayout2@requestLayout
D/logfish: AFrameLayout2@onLayout
D/logfish: AFrameLayout2@requestLayout
D/DemoActivity2: @onGlobalLayout A1: true A2: true

ohye,成功得让A1,A2位true了,实现了我们的期望。此时layout已经结束(onGlobalLayout相关知识参考onGlobalLayout的触发),但是A1,A2的PFLAG_FORCE_LAYOUT标志位还是1,这代码他们的子view的requestlayout都会无效,我们试一试点击textview A0。代码如下,可以看到应该显示 但是we are family。

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("we are family");
            }
        });

但是结果如下所示,只显示了一个we。为什么?
我们的TextView是wrap_content的,此时的setText会调用invalidate和requestlayout,但是requestLayout没有申请到vsync,所以不会重新layout,只是重新draw了一遍,所以textview的宽度不会改变,原来的宽只够放“we”,所以这里就显示了we。

RequestLayoutInLayout_demo_text.pngenter description here

此时我们比较形象的呈现了这个bug,只要打开DemoActivity2,点一下switch,再点一下下面的textview就会出现bug。这种bug往往解决起来比较费劲,不好定位,所以下面给出一套检测方案。

检测方案使用

activity:DemoActivity3
主要接口如下,使用方法很简单,在Activity的onCreate里调用start,在Activity的onDestroy里调用stop。然后重新跟布局的forceLayout方法,通知
RequestAbnormalDetector。


public class RequestAbnormalDetector {
    //Activity onCreate
    public static void start(Activity activity) {
        ...
    }
    //Activity onDestroy
    public static void stop(Activity activity) {
        ...
    }

    //used in root view
    public static void afterForceLayout(View view) {
        ...
    }

    public static void preRequestLayout(View abnormalView) {
        ...
    }

    //usually on when Debug off when release
    public static void setOn(boolean pOn) {
        ...
    }

}

跟布局的forceLayout如下.主要目的是通知RequestAbnormalDetector。

   @Override
    public void forceLayout() {
        super.forceLayout();
        //tell RequestAbnormalDetector
        RequestAbnormalDetector.afterForceLayout(this);
    }

此时我们运行DemoActivity3,并且点击switch按钮,可以看到日志会报警。这段日志什么意思?这里重点是第6句,指明了AFrameLayout2有异常,需要preRequest,所以我们改写AFrameLayout2。

 D/DetectorImpl: @onGlobalLayout abnormal view start------> 
 W/DetectorImpl: depth: 3 PFLAG_FORCE_LAYOUT:true view: com.fish.requestlayoutinlayout.view.ALinearLayout1{531e00a0 
 W/DetectorImpl: depth: 2 PFLAG_FORCE_LAYOUT:true view: com.fish.requestlayoutinlayout.view.AFrameLayout2{531d245c 
 W/DetectorImpl: depth: 1 PFLAG_FORCE_LAYOUT:false view:  V.E..... ......ID 0,0-768,1022 #7f0b005f app:id/a3}
 W/DetectorImpl: depth: 0 PFLAG_FORCE_LAYOUT:false view:  V.E..... ......ID 0,0-768,1022 #7f0b005e app:id/root}
 E/DetectorImpl: please preRequest this: com.fish.requestlayoutinlayout.view.AFrameLayout2{531d245c V.E..... ish.requestlayoutinlayout.DemoActivity3@531d725c
 D/DetectorImpl: @onGlobalLayout abnormal view end<------ 

只是改写requestLayout代码。然后我们再编译运行。

    @Override
    public void requestLayout() {
        RequestAbnormalDetector.preRequestLayout(this);
        super.requestLayout();
        LogUtil.d("AFrameLayout2@requestLayout");
    }

打开DemoActivity3,然后我们可以看到堆栈如下,我们可以很明显的看到在AFrameLayout3_V2.onLayout里调用了ALinearLayout1.requestLayout。这样就找到了requestlayout in layout的代码位置,我们就可以针对这个来修bug了。

om.fish.requestlayoutinlayout.detect.RequestLayoutException
          at com.fish.requestlayoutinlayout.detect.RequestAbnormalDetector.preRequestLayout(RequestAbnormalDetector.java:71)
          at com.fish.requestlayoutinlayout.view.AFrameLayout2.requestLayout(AFrameLayout2.java:32)
          at android.view.View.requestLayout(View.java:15473)
          at com.fish.requestlayoutinlayout.view.ALinearLayout1.requestLayout(ALinearLayout1.java:31)
          at android.view.View.setLayoutParams(View.java:10029)
          at com.fish.requestlayoutinlayout.view.AFrameLayout3_V2.onLayout(AFrameLayout3_V2.java:61)
          at android.view.View.layout(View.java:14008)
          at android.view.ViewGroup.layout(ViewGroup.java:4373)
          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
          at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1652)
          at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
          at android.view.View.layout(View.java:14008)
          at android.view.ViewGroup.layout(ViewGroup.java:4373)
          at android.widget.FrameLayout.onLayout(FrameLayout.java:448)

猜你喜欢

转载自blog.csdn.net/litefish/article/details/78821435