AppBarLayout中的悬浮部分我使用了MagicIndicator,我们的测试在实际的使用中发现,MagicIndicator置顶之后再滑动Fragment中的RecyclerView时,在RecyclerView还在向上惯性滑动的时候向下滑动MagicIndicator,这时MagicIndicator也可以滑动,从而导致MagicIndicator和RecyclerView会对向滑动;
我们的需求是MagicIndicator吸顶之后,RecyclerView在滑动过程中MagicIndicator不能滑动
针对这个问题,我使用了AppBarLayout的setDragCallback()方法来解决:
通过对Fragment中RecyclerView的滑动距离进行监听,当滑动距离<=0的时候,表明MagicIndicator已经吸顶了,这时候就禁止掉AppBarLayout的滑动;反之则开启AppBarLayout的滑动
val params = binding.appbarlayout.layoutParams as CoordinatorLayout.LayoutParams
val behavior = params.behavior as? AppBarLayout.Behavior
behavior?.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {
override fun canDrag(@NonNull appBarLayout: AppBarLayout): Boolean {
return onlyRvCommentScrollY <= 0 //如果底部列表有滑动距离,就不让tab栏进行滑动
}
})