前言
LinearSnapHelper与PagerSnapHelper是一个滑动自动回正的一个辅助类,一般多用于RecyclerView的滚轮选择器的开发上
LinearSnapHelper与PagerSnapHelper的区别, LinearSnapHelper多用于一页显示多行内容的线性列表,PagerSnapHelper用于当前只显示一页内容的翻页列表
转载请注明来源:https://www.cnblogs.com/guanxinjing/p/16276217.html
LinearSnapHelper的使用
效果图
设置LinearSnapHelper代码
val provinceLinearSnapHelper = LinearSnapHelper()
provinceLinearSnapHelper.attachToRecyclerView(mRecyclerView)
监听当前滚动的居中位置,注意是的居中位置
mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { super.onScrollStateChanged(recyclerView, newState) if (newState == RecyclerView.SCROLL_STATE_IDLE) { if (recyclerView != null && recyclerView.childCount > 0) { provinceLinearSnapHelper.findSnapView(mBinding.province.layoutManager)?.let { val position = recyclerView.getChildAdapterPosition(it) } } } } })
PagerSnapHelper
设置代码
val pagerSnapHelper = PagerSnapHelper()
pagerSnapHelper.attachToRecyclerView(mBinding.province)
End