Android Material Design - How to change background color of Toolbar after CollapsingToolbarLayout is collapsed

前端 未结 6 1845
情歌与酒
情歌与酒 2020-12-07 22:54

After the user scrolls down the screen, the image in the CollapsingToolbarLayout disappears and is left with a toolbar with the back button, content title,

6条回答
  •  情书的邮戳
    2020-12-07 23:27

    You can use an AppBarLayout's offset listener and change the CollapsingTollbar attributes according to the desired behavior.

    appBarLayout.addOnOffsetChangedListener { _, verticalOffSet ->
                if (Math.abs(verticalOffSet) == appBarLayout.totalScrollRange) {
                    //Collapsed
                    toolBar.setBackgroundDrawable(ContextCompat.getDrawable(this,
                            R.drawable.last_revolut_gradient))
                } else {
                    //Expanded
                    toolBar.setBackgroundColor(ContextCompat.getColor(this,
                            android.R.color.transparent))
                }
            }
    

提交回复
热议问题