CollapsingToolbarLayout里面添加textview,一直重绘布局报错

本文详细解析了在使用AppBarLayout时遇到的TextView布局重绘错误,并提供了有效的解决方案。通过添加OnOffsetChangedListener监听器,调整TextView的Drawable状态,避免了布局在第二轮布局过程中的不当调用。

布局如下

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:expanded="false">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bgTextActive"
        app:contentScrim="@color/bgTextActive"
        app:expandedTitleGravity="top"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:scrimAnimationDuration="0">

        <include
            layout="@layout/monthpicker"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp" />

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolBar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            app:layout_collapseMode="pin">
            <RelativeLayout
                android:id="@+id/ll_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/tv_date" 。  //--------------look here---------
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"
                    android:drawableRight="@drawable/down_white"
                    android:drawablePadding="2dp"
                    android:textColor="@color/white"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:text="2020-03"
                    android:textSize="18sp" />
                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:visibility="gone"
                    android:id="@+id/iv_filter"
                    android:src="@drawable/unset_normal"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"/>
            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

一直刷日志报错:

requestLayout() improperly called by androidx.appcompat.widget.AppCompatTextView{bd68872 VFED..C.. ........ 396,0-639,138 #7f080188 app:
id/tv_date} during second layout pass: posting in next frame
2020-03-27 10:22:25.405 12191-12191/com.uwei.mrecord W/View:

solution

collapseLayout.addOnOffsetChangedListener(new AppBarLayout.BaseOnOffsetChangedListener() {
    int lastOffset = -1;                //--------- look here ----- 
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (lastOffset == verticalOffset) return;
        lastOffset = verticalOffset;    //---------- and here   -----
        if (verticalOffset == -collapseLayout.getHeight() + toolbar.getHeight()) {
            isOpenYear = false;
            tvDate.setCompoundDrawables(null,null,drawableDown,null);
        }else {
            isOpenYear = true;
            tvDate.setCompoundDrawables(null,null,drawableUp,null);
        }
    }
});

ok.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值