requestLayout()调用不正确

时间:2016-07-01 00:22:53

标签: android android-view android-support-library android-toolbar android-collapsingtoolbarlayout

我在控制台中收到此警告。我能找到的唯一信息是大多数人在列表视图中启用快速滚动时获得它。我没有使用任何列表视图。它说它被CollapsingToolbarLayout,AppCompatTextView,Toolbar和AppBarLayout不正确地调用。只要我快速“匆匆忙忙”,就会发生这种情况。我的嵌套滚动视图向上和向下折叠并展开工具栏。

日志:

06-30 22:18:42.622 19090-19090/? I/art: Not late-enabling -Xcheck:jni (already on)
06-30 22:18:43.153 19090-19090/joebruckner.lastpick.debug D/MovieShuffleActivity: false, true, false
06-30 22:18:43.205 19090-19126/joebruckner.lastpick.debug D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                            [ 06-30 22:18:43.267 19090:19126 D/         ]
                                                                            HostConnection::get() New Host Connection established 0xea95fdb0, tid 19126
06-30 22:18:43.287 19090-19126/joebruckner.lastpick.debug I/OpenGLRenderer: Initialized EGL, version 1.4
06-30 22:18:43.806 19090-19090/joebruckner.lastpick.debug D/MovieShuffleActivity: false, true, true
06-30 22:28:49.264 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ......ID 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during layout: running second layout pass
06-30 22:28:49.264 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.v7.widget.Toolbar{612299e V.E...... ......ID 0,291-1080,438 #7f0d0069 app:id/toolbar} during layout: running second layout pass
06-30 22:28:49.277 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ........ 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during second layout pass: posting in next frame
06-30 22:28:49.456 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ......ID 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during layout: running second layout pass
06-30 22:28:49.456 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.v7.widget.Toolbar{612299e V.E...... ......ID 0,197-1080,344 #7f0d0069 app:id/toolbar} during layout: running second layout pass
06-30 22:28:49.475 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ........ 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during second layout pass: posting in next frame
06-30 22:28:49.696 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ......ID 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during layout: running second layout pass
06-30 22:28:49.696 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.v7.widget.Toolbar{612299e V.E...... ......ID 0,0-1080,147 #7f0d0069 app:id/toolbar} during layout: running second layout pass
06-30 22:28:49.741 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ........ 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during second layout pass: posting in next frame

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:titleEnabled="false"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:contentDescription="@string/backdrop"
                android:scaleType="fitXY"  />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appBar"
        app:layout_behavior="@string/scrolling_view"
        app:behavior_overlapTop="80dp"
        />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_dice_three_48dp"
        app:layout_anchor="@+id/frame"
        app:layout_anchorGravity="bottom|end"
        />
</android.support.design.widget.CoordinatorLayout>

6 个答案:

答案 0 :(得分:3)

我的解决方案是仅在偏差变化

时转发偏移量变化
       appbar.addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener {
        var lastOffset = -1
        override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
            if (lastOffset == verticalOffset) return

            lastOffset = verticalOffset
            val percentage = Math.abs(verticalOffset).toFloat() / (appBarLayout?.totalScrollRange?.toFloat() ?: 1f)
            toolbarAnimation.animate(percentage)
        }
    })

答案 1 :(得分:2)

您是否正在设置任何AppBarLayout.OnOffsetChangedListener?如果是这样,可能会导致布局参数归属或此类型在侦听器中的任何设定值。

如果是这种情况,请在主线程中设置布局属性

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

           //post message back on main thread if you need to modify any views
        }
    });

答案 2 :(得分:2)

此消息仅用作某个视图已请求布局以及其他视图(在您的情况下为ToolbarCollapsingToolbarLayout)已在布局阶段请求布局的警告。

在某些情况下,这会导致性能下降。你应该确定:

  1. 您是否有一些在滚动期间调用View的自定义ViewGroup / requestLayout()?你有其他调用requestLayout()的代码吗?您是否有任何代码可以进行广泛的布局子级别更改,例如查看GONE

  2. 这是什么时候发生的?只是在开始时,就在所有观点布局之前?

  3. 您有可疑的观点,requestLayout()调用不正确,您需要找到之前,何时以及为何进行布局重组。

答案 3 :(得分:1)

当适配器的getView()方法显示您的布局而某些其他代码试图访问此视图以显示它时,可能会发生这种情况,从而导致冲突。 请检查。

答案 4 :(得分:0)

将标题设置为CollapsingToolbarLayout而不是Toolbar帮助我解决了这个问题

collapsingToolbarLayout.setTitle(title);

而不是

toolbar.setTitle(title);

答案 5 :(得分:0)

对于 kotlin 如果您在此处设置任何视图的值,它将被 requestLayout 进程警告淹没。 您可以尝试使用视图可见性 gone / visible

private fun addListener(){
        var isShow = false
        var scrollRange = -1
        tvUserTitle.text = "Hasnine"

        ioCoroutineScope.launch {
            appBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { barLayout, verticalOffset ->


                if (scrollRange == -1) {
                    scrollRange = barLayout?.totalScrollRange!!
                }
                if (scrollRange + verticalOffset == 0) {
                    //tvTitleHome.title = "Hasnine"
                    //toolbarCollapse.title = "hello world"

                    isShow = true
                    showTitle(isShow)

                } else if (isShow) {
                    //tvTitleHome.title = "" //careful there should a space between double quote otherwise it wont work

                    isShow = false
                    showTitle(isShow)
                }
            })
        }
}

private fun showTitle(show: Boolean){
        if(show){
            tvUserTitle.visibility = View.VISIBLE
        }else{
            tvUserTitle.visibility = View.GONE
        }
    }