监听事件
barLay.addOnOffsetChangedListener(this)
override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
Log.e("height", verticalOffset.toString())
val verticalHeight = abs(verticalOffset)
val collHeight = rlCollLay.height
if (verticalHeight >= collHeight - 5 ){
tvAcTitle.text = "个人主页"
}else{
tvAcTitle.text = ""
}
//这个就是关键方法了,大家可根据实际需要进行编写
if (verticalOffset < 0){
when (verticalHeight) {
in 0..10 -> {//最好多写一点儿,避免出现问题
coordLay.setBackgroundColor(ContextCompat.getColor(this,R.color.transparent))
}
in 1..collHeight -> {
val scale: Float = verticalOffset.toFloat() / collHeight
val alpha = 255 * (1 - scale)
coordLay.setBackgroundColor(Color.argb(alpha.toInt(), 255, 255, 255))
}
else ->{
coordLay.setBackgroundColor(ContextCompat.getColor(this,R.color.transparent))
}
}
}
//追加一个判断,避免存在一点儿朦层效果
if (verticalHeight in 0..10){
coordLay.setBackgroundColor(ContextCompat.getColor(this,R.color.transparent))
}
}
网友评论