CoordinatorLayout+AppBarLayout 布局中实现自动滚动到顶部的效果:
fun scrollToTop() {
binding.appBarLayout.apply {
val behavior = (layoutParams as CoordinatorLayout.LayoutParams).behavior
if (behavior is AppBarLayout.Behavior) {
behavior.topAndBottomOffset = 0 //快熟滑动到顶部
// behavior.topAndBottomOffset = -height //快速滑动实现吸顶效果
}
}
}