滚动时改变工具栏颜色而不折叠

4
我正在尝试在滚动时改变我的工具栏的颜色,而不实际折叠它(因此我不希望其高度改变)。 我希望它最初是一种颜色,然后在滚动时会更改为另一种颜色。 当我滚回到顶部时,它应该恢复到原始颜色。
我已经能够在滚动时更改颜色,但只与高度的变化相结合。 我尝试在Toolbar、CollapsingToolbarLayout和AppBarLayout上设置minHeight,但不幸的是没有成功。
我知道我可以手动完成这个过程(在滚动更改时动画颜色),但我想先排除使用设计库来完成它。
这是我目前拥有的:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/app_secondary_darker">

    <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:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/white"
            app:layout_scrollFlags="scroll">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:minHeight="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:text="Abc"
                android:background="@color/grey_light"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:text="Def"
                android:background="@color/red_dark"/>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

提前感谢您!

1个回答

0

希望有所帮助。

app_bar.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
    val collapsedPercent = -verticalOffset / appBarLayout.totalScrollRange.toFloat()

    //仅在这 0.3的区间里交互 //这个值 在  (0,1]之间,可以自定义
    //这里表示仅在收缩到仅剩百分比hotPercent的时候,才进行收缩
    val hotPercent = 0.3F

    val collapsedHotPercent = MathUtils.clamp(collapsedPercent / hotPercent + 1 - 1 / hotPercent, 0F, 1F)

    Log.d(TAG, "onCreate collapsedPercent:" + collapsedPercent)

    tabs.apply {
        val normalColorStart = ContextCompat.getColor(context, R.color.k6_main_tabs_normal_start)
        val normalColorEnd = ContextCompat.getColor(context, R.color.k6_main_tabs_normal_end)
        val selectedColorStart = ContextCompat.getColor(context, R.color.k6_main_tabs_selected_start)
        val selectedColorEnd = ContextCompat.getColor(context, R.color.k6_main_tabs_selected_end)

        val normalColor = ColorUtil.getColorOfDegradate(normalColorStart, normalColorEnd, collapsedHotPercent)
        val selectedColor = ColorUtil.getColorOfDegradate(selectedColorStart, selectedColorEnd, collapsedHotPercent)

        setTabTextColors(normalColor, selectedColor)
        setSelectedTabIndicatorColor(selectedColor)
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接