使用可折叠工具栏和页面适配器时请求布局调整不正确

时间:2017-01-06 14:41:15

标签: android view

我得到了以下错误的无限循环:

W/View: requestLayout() improperly called by android.support.v7.widget.AppCompatTextView{9968cfe V.ED..... ......ID 64,0-1376,301 #7f0c008a app:id/project_name} during layout: running second layout pass
I/ViewRootImpl: requestLayoutDuringLayout is already in process
I/ViewRootImpl: requestLayoutDuringLayout is already in process
I/ViewRootImpl: requestLayoutDuringLayout is already in process

我的CoordinatorLayoutCollapsingToolbarLayout中包含AppBarLayout。在AppBarLayout之外,我有一个ViewPager。当CollapsingToolbarLayout折叠时,上面的错误消息开始无休止地循环。

我在CollapsingToolbarLayout关闭时有一个监听器,这样我就可以隐藏并显示一些视图。

@Override public void onOffsetChanged(final AppBarLayout appBarLayout, final int verticalOffset) {
    if (scrollRange == -1) {
        scrollRange = appBarLayout.getTotalScrollRange();
    }

    if (scrollRange + verticalOffset == 0 && !isShow) {
        view.showToolbarTitle(true);
        isShow = true;
    } else if (isShow) {
        view.showToolbarTitle(false);
        isShow = false;
    }
}

当我删除此侦听器时,错误会停止。我假设正在发生的事情是onOffsetChanged(AppBarLayout, int)CollapsingToolbarLayout崩溃时不断被一遍又一遍地调用。但这没有任何意义,因为没有任何偏移被改变。有没有人知道为什么会这样?

1 个答案:

答案 0 :(得分: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
        }
    }