CoordinatorLayout 嵌套 AppBarLayout RecyclerView ,通过代码控制,使得CoordinatorLayout 自动滑动到tab置顶的位置


有两个方式可以实现

一:调用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滚到到顶部后无法下拉

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM