有兩個方式可以實現
一:調用AppBarLayout,設置間距
val behavior = (appbar_layout.getLayoutParams() as CoordinatorLayout.LayoutParams).getBehavior() if (behavior is AppBarLayout.Behavior) { var appBarLayoutBehavior = behavior var topAndBottomOffset = appBarLayoutBehavior.topAndBottomOffset; if (topAndBottomOffset <= 0) { appBarLayoutBehavior.topAndBottomOffset = - layout_root.y.toInt() } }
二:調用現有的api
appbar_layout.setExpanded(false)
方法二更方便更好
三:
滑動到頂部
appbar_layout.setExpanded(true) recycler_view.stopScroll() recycler_view.scrollToPosition(0)
必須加入
recycler_view的滾動,不然appbar滾到到頂部后無法下拉