RecyclerVIew->加速再减速的RecyclerVIew平滑对齐工具类SnapHelper

XML文件

  • ItemViewXML文件R.layout.shape_item_view
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="100dp"android:layout_height="100dp"android:background="@drawable/shape_item_view"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:textSize="16sp" />
</FrameLayout>
  • 滑动到对齐ItemViewXML文件 R.drawable.shape_item_view_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#FFFFFF" /><corners android:radius="8dp" /><stroke android:color="#FFFF00" android:width="5dp" />
</shape>
  • 未滑动到对齐ItemViewXML文件 R.drawable.shape_item_view
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#FFFFFF" /><corners android:radius="8dp" /><stroke android:color="#000000" android:width="5dp" />
</shape>
  • Activity的XML文件R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="100dp"android:layout_marginLeft="@dimen/edit_crop_frame_padding"android:layout_marginRight="@dimen/edit_crop_frame_padding"android:orientation="horizontal" />
</LinearLayout>

RecyclerView代码

  • Adapter代码
class MyAdapter(private val numbers: List<Int>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {// 记录选中位置private var selectedPosition = RecyclerView.NO_POSITIONoverride fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)return MyViewHolder(view)}override fun onBindViewHolder(holder: MyViewHolder, position: Int) {holder.textView.text = numbers[position].toString()if (selectedPosition == position) {holder.itemView.setBackgroundResource(R.drawable.shape_item_view_selected)} else {holder.itemView.setBackgroundResource(R.drawable.shape_item_view)}}override fun getItemCount() = numbers.size// 给外部工具类SnapHelper实现类使用,滚动到对齐位置,修改ItemView的轮廓fun setSelectedPosition(position: Int) {val oldPosition = selectedPositionselectedPosition = positionnotifyItemChanged(oldPosition)notifyItemChanged(selectedPosition)}class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {val textView: TextView = itemView.findViewById(R.id.textView)}
}
  • ItemDecoration代码
class SpaceItemDecoration(private val spaceSize: Int, private val itemSize : Int) : RecyclerView.ItemDecoration() {private val paint = Paint().apply {color = Color.REDstyle = Paint.Style.FILL}var spacePadding = 0override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {super.getItemOffsets(outRect, view, parent, state)// 正常Item的DecorationoutRect.left = spaceSizeoutRect.right = spaceSize// 第一个和最后一个Item的DecorationspacePadding = (parent.measuredWidth / 2 - itemSize / 2)val size = parent.adapter?.itemCount ?: 0val position = parent.getChildAdapterPosition(view)if (position == 0) {outRect.left = spacePadding} else if (position == size - 1) {outRect.right = spacePadding}}override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {super.onDraw(c, parent, state)val childCount = parent.childCountfor (i in 0 until childCount) {val child = parent.getChildAt(i)val position = parent.getChildAdapterPosition(child)val params = child.layoutParams as RecyclerView.LayoutParamsvar left : Intvar right : Intvar top : Intvar bottom : Intif (position == 0) {left = child.left - params.leftMargin - spacePaddingright = child.right + params.rightMargin + spaceSizetop = child.top - params.topMarginbottom = child.bottom + params.bottomMargin} else if (position == parent.adapter?.itemCount!! - 1) {left = child.left - params.leftMargin - spaceSizeright = child.right + params.rightMargin + spacePaddingtop = child.top - params.topMarginbottom = child.bottom + params.bottomMargin} else {// 绘制其他 Item 的装饰left = child.left - params.leftMargin - spaceSizeright = child.right + params.rightMargin + spaceSizetop = child.top - params.topMarginbottom = child.bottom + params.bottomMargin}c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)}}
}

RecyclerView对齐工具类SnapHelper实现类代码

  • attachToRecyclerView()方法:将RecyclerView对齐操作交给SnapHelper实现类
override fun attachToRecyclerView(recyclerView: RecyclerView?) {Log.i(TAG, "attachToRecyclerView")mRecyclerView = recyclerViewsuper.attachToRecyclerView(recyclerView)
}
  • createScroller()方法:创建惯性滑动的Scroller
    • onTargetFound()回调方法:找到对齐位置之后回调,计算目标位置到对齐位置需要滚动的距离和时间
    • calculateSpeedPerPixel()回调方法:滚动一英寸所需时间除以屏幕密度,得到滚动一像素所需的时间
override fun createScroller(layoutManager: RecyclerView.LayoutManager?): RecyclerView.SmoothScroller? {if (layoutManager !is RecyclerView.SmoothScroller.ScrollVectorProvider) {return null}Log.i(TAG, "createScroller")return object : LinearSmoothScroller(mRecyclerView?.context) {override fun onTargetFound(targetView: View, state: RecyclerView.State, action: Action) {if (mRecyclerView == null) returnLog.i(TAG, "onTargetFound")// 计算当前位置到目标位置的距离val snapDistances : IntArray? = calculateDistanceToFinalSnap(mRecyclerView?.layoutManager!!, targetView)val dx = snapDistances?.get(0) ?: 0val dy = snapDistances?.get(1) ?: 0// 这里增加滑动时间,可以使得滑动速度变慢val time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy))) * 10if (time > 0) {// 这里传入的是LinearOutSlowInInterpolator(), 也就是先加速再减速的插值器// 相比LinearSnapHelper中的DecelerateInterpolator, 这个插值器更符合自然滑动的效果action.update(dx, dy, time, mInterpolator)}}override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float {Log.i(TAG, "calculateSpeedPerPixel")// 计算滑动一个像素的时间return MILLISECONDS_PER_INCH / displayMetrics?.densityDpi!!}}
}
  • findTargetSnapPosition()方法:找到需要对齐的ItemView的位置
    • RecyclerView.SmoothScroller.ScrollVectorProvider.computeScrollVectorForPosition():计算从0的位置滚动到ItemCount-1的位置需要滚动的方向,vectorForEnd.x表示水平方向(>0向右,<0向左),vectorForEnd.y表示竖直方向(>0向下,<0向上),正负值由结束位置和开始位置的差值得出
override fun findTargetSnapPosition(layoutManager: RecyclerView.LayoutManager?, velocityX: Int, velocityY: Int): Int {// 判断layoutManager是否实现了RecyclerView.SmoothScroller.ScrollVectorProvider这个接口if (layoutManager !is RecyclerView.SmoothScroller.ScrollVectorProvider) return RecyclerView.NO_POSITION// 判断ItemView个数是否小于等于0val itemCount = layoutManager.itemCountif (itemCount == 0) return RecyclerView.NO_POSITION// 找到需要对齐的ItemViewval currentView = findSnapView(layoutManager) ?: return RecyclerView.NO_POSITION// 获取需要对齐ItemView的位置val currentPosition = layoutManager.getPosition(currentView)if (currentPosition == RecyclerView.NO_POSITION) return RecyclerView.NO_POSITION// 判断layoutManager的布局方向val vectorProvider = layoutManager as RecyclerView.SmoothScroller.ScrollVectorProviderval vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1) ?: return RecyclerView.NO_POSITIONLog.i(TAG, "findTargetSnapPosition")// 计算水平, 垂直方向最多能惯性滑动的ItemView个数,在当前ItemView的位置上,进行加减Position操作var maxHorizontalItemViewCount: Intvar maxVerticalItemViewCount: Intif (layoutManager.canScrollHorizontally()) {maxHorizontalItemViewCount = estimateNextPositionDiffForFling(layoutManager, getHorizontalHelper(layoutManager), velocityX, 0)var sign = Math.signum(velocityX.toFloat())if (sign == 0f) sign = 1f// 限制最多能惯性滑动的ItemView个数,限制滑动的 ItemView 的个数在 1 到 2 之间maxHorizontalItemViewCount = (sign * Math.min(Math.max(Math.abs(maxHorizontalItemViewCount), 0), 2)).toInt()if (vectorForEnd.x < 0) {maxHorizontalItemViewCount = - maxHorizontalItemViewCount}}else{maxHorizontalItemViewCount = 0}if (layoutManager.canScrollVertically()) {maxVerticalItemViewCount = estimateNextPositionDiffForFling(layoutManager, getVerticalHelper(layoutManager), 0, velocityY)var sign = Math.signum(velocityY.toFloat())if (sign == 0f) sign = 1f// 限制最多能惯性滑动的ItemView个数,限制滑动的 ItemView 的个数在 1 到 2 之间maxVerticalItemViewCount = (sign * Math.min(Math.max(Math.abs(maxVerticalItemViewCount), 0), 2)).toInt()if (vectorForEnd.y < 0) {maxVerticalItemViewCount = - maxVerticalItemViewCount}}else{maxVerticalItemViewCount = 0}// 根据滑动的方向,计算出最终的 ItemView 个数val finalItemCount = if(layoutManager.canScrollHorizontally()){maxHorizontalItemViewCount}else{maxVerticalItemViewCount}if (finalItemCount == 0) return RecyclerView.NO_POSITION// 确定最终的对齐位置,并做边界处理var targetPosition = currentPosition + finalItemCountif (targetPosition < 0) targetPosition = 0if (targetPosition >= layoutManager.itemCount) targetPosition = layoutManager.itemCount - 1return targetPosition
}
  • findSnapView()方法:调用findCenterView()找到最接近中心点的ItemView
    • findCenterView()方法:拿到每个ItemViewleft加上自身宽度的一半和RecyclerView的中心点进行比较,找到最接近中心点的ItemView
override fun findSnapView(layoutManager: RecyclerView.LayoutManager?): View? {Log.i(TAG, "findSnapView")if (layoutManager!!.canScrollVertically()) {return findCenterView(layoutManager, getVerticalHelper(layoutManager))} else if (layoutManager.canScrollHorizontally()) {return findCenterView(layoutManager, getHorizontalHelper(layoutManager))}return null
}
private fun findCenterView(layoutManager: RecyclerView.LayoutManager, helper: OrientationHelper): View? {Log.i(TAG, "findCenterView")val childCount = layoutManager.childCountif (childCount == 0) return null// 最接近RecyclerView中心的ItemViewvar closestItemView: View? = null// RecyclerView的中心点val center = helper.startAfterPadding + helper.totalSpace / 2var absClosest = Int.MAX_VALUEfor (i in 0 until childCount) {val child = layoutManager.getChildAt(i)// ItemView的中心点, 这里用Left是因为有ItemDecoration的存在,val childCenter = child?.left!! + helper.getDecoratedMeasurement(child) / 2val childDistance = Math.abs(childCenter - center)// 找到最靠近RecyclerView中心的ItemViewif (childDistance < absClosest) {absClosest = childDistanceclosestItemView = child}}return closestItemView
}
  • estimateNextPositionDiffForFling()方法:计算当前位置到目标对齐位置还差了几个ItemView的个数
    • calculateScrollDistance():计算RecyclerView的滚动距离
    • computeDistancePerChild():计算每个ItemView的滚动距离
private fun estimateNextPositionDiffForFling(layoutManager: RecyclerView.LayoutManager, helper: OrientationHelper, velocityX: Int, velocityY: Int): Int {Log.i(TAG, "estimateNextPositionDiffForFling")val distances = calculateScrollDistance(velocityX, velocityY)val distancePerChild = computeDistancePerChild(layoutManager, helper)if (distancePerChild <= 0) return 0val distance = if (Math.abs(distances[0]) > Math.abs(distances[1])) distances[0] else distances[1]return Math.round(distance / distancePerChild)
}
override fun calculateScrollDistance(velocityX: Int, velocityY: Int): IntArray {Log.i(TAG, "calculateScrollDistance")return super.calculateScrollDistance(velocityX, velocityY)
}
private fun computeDistancePerChild(layoutManager: RecyclerView.LayoutManager, helper: OrientationHelper): Float {Log.i(TAG, "computeDistancePerChild")var minPositionView : View ?= nullvar maxPositionView : View ?= nullvar minPosition = Integer.MAX_VALUEvar maxPosition = Integer.MIN_VALUEval itemViewCount = layoutManager.childCountif (itemViewCount == 0) return INVALID_DISTANCE// 遍历所有ItemView, 找到最小位置和最大位置的ItemView,记录Positionfor (i in 0 until itemViewCount) {val child = layoutManager.getChildAt(i) ?: continueval position = layoutManager.getPosition(child)if (position == RecyclerView.NO_POSITION) continueif (position < minPosition) {minPosition = positionminPositionView = child}if (position > maxPosition) {maxPosition = positionmaxPositionView = child}}if (minPositionView == null || maxPositionView == null) return INVALID_DISTANCE// 计算最小位置和最大位置的ItemView离RecyclerView左边的距离val start = Math.min(helper.getDecoratedStart(minPositionView), helper.getDecoratedStart(maxPositionView))// 计算最小位置和最大位置的ItemView离RecyclerViewj右边的距离val end = Math.max(helper.getDecoratedEnd(minPositionView), helper.getDecoratedEnd(maxPositionView))// 计算最小位置和最大位置的ItemView的宽度val distance = end - startif (distance <= 0) return INVALID_DISTANCEreturn 1f * distance / (maxPosition - minPosition + 1)
}
  • onTargetFound()方法:找对对齐ItemView位置后回调
    • calculateDistanceToFinalSnap():计算最终需要滚动到对齐ItemView位置的距离
    • calculateTimeForDeceleration():计算最终需要滚动到对齐ItemView位置所花时间
override fun onTargetFound(targetView: View, state: RecyclerView.State, action: Action) {if (mRecyclerView == null) returnLog.i(TAG, "onTargetFound")// 计算当前位置到目标位置的距离val snapDistances : IntArray? = calculateDistanceToFinalSnap(mRecyclerView?.layoutManager!!, targetView)val dx = snapDistances?.get(0) ?: 0val dy = snapDistances?.get(1) ?: 0// 这里增加滑动时间,可以使得滑动速度变慢val time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy))) * 10if (time > 0) {// 这里传入的是LinearOutSlowInInterpolator(), 也就是先加速再减速的插值器// 相比LinearSnapHelper中的DecelerateInterpolator, 这个插值器更符合自然滑动的效果action.update(dx, dy, time, mInterpolator)}
}
override fun calculateDistanceToFinalSnap(layoutManager: RecyclerView.LayoutManager, targetView: View): IntArray? {Log.i(TAG, "calculateDistanceToFinalSnap")// 计算当前位置到目标位置的距离val out = IntArray(2)if (layoutManager.canScrollHorizontally()) {out[0] = distanceToCenter(targetView, getHorizontalHelper(layoutManager)!!)} else {out[0] = 0}if (layoutManager.canScrollVertically()) {out[1] = distanceToCenter(targetView, getVerticalHelper(layoutManager)!!)} else {out[1] = 0}return out
}
  • distanceToCenter()方法:计算目标对齐ItemView距离RecyclerView中心点的距离
private fun distanceToCenter(targetView: View, helper: OrientationHelper): Int {Log.i(TAG, "distanceToCenter")// 计算目标ItemView的中心点(ItemView包含ItemDecoration的部分 + ItemView的宽度/高度的一半)的val childCenter = helper.getDecoratedStart(targetView) + helper.getDecoratedMeasurement(targetView) / 2// 计算RecyclerView的中心点(RecyclerView减去Padding的部分 + RecyclerView的宽度/高度的一半)val containerCenter = helper.startAfterPadding + helper.totalSpace / 2return childCenter - containerCenter
}
  • 完整加减速的对齐工具类SnapHelper的代码
open class MySmoothSnapHelper : SnapHelper() {private val INVALID_DISTANCE = 1f // 无法计算有效对齐距离,返回这个值private val MILLISECONDS_PER_INCH = 25f // 滑动速度,每英寸25毫秒// 通过LayoutManager创建方向工具类,其中包含了RecyclerView的布局参数,包括padding,margin等private var mVerticalHelper : OrientationHelper ?= nullprivate var mHorizontalHelper : OrientationHelper ?= nullprivate var mRecyclerView : RecyclerView ?= null// 加速->减速插值器private val mInterpolator = LinearOutSlowInInterpolator()// 将RecyclerView交给SnapHelper, 计算惯性滑动后需要对齐的位置override fun attachToRecyclerView(recyclerView: RecyclerView?) {Log.i(TAG, "attachToRecyclerView")mRecyclerView = recyclerViewsuper.attachToRecyclerView(recyclerView)}// 创建惯性滑动的Scrolleroverride fun createScroller(layoutManager: RecyclerView.LayoutManager?): RecyclerView.SmoothScroller? {Log.i(TAG, "createScroller")if (layoutManager !is RecyclerView.SmoothScroller.ScrollVectorProvider) {return null}return object : LinearSmoothScroller(mRecyclerView?.context) {override fun onTargetFound(targetView: View, state: RecyclerView.State, action: Action) {if (mRecyclerView == null) returnLog.i(TAG, "onTargetFound")// 计算当前位置到目标位置的距离val snapDistances : IntArray? = calculateDistanceToFinalSnap(mRecyclerView?.layoutManager!!, targetView)val dx = snapDistances?.get(0) ?: 0val dy = snapDistances?.get(1) ?: 0// 这里增加滑动时间,可以使得滑动速度变慢val time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy))) * 10if (time > 0) {// 这里传入的是LinearOutSlowInInterpolator(), 也就是先加速再减速的插值器// 相比LinearSnapHelper中的DecelerateInterpolator, 这个插值器更符合自然滑动的效果action.update(dx, dy, time, mInterpolator)}}override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float {Log.i(TAG, "calculateSpeedPerPixel")// 计算滑动一个像素的时间return MILLISECONDS_PER_INCH / displayMetrics?.densityDpi!!}}}override fun calculateDistanceToFinalSnap(layoutManager: RecyclerView.LayoutManager, targetView: View): IntArray? {Log.i(TAG, "calculateDistanceToFinalSnap")// 计算当前位置到目标位置的距离val out = IntArray(2)if (layoutManager.canScrollHorizontally()) {out[0] = distanceToCenter(targetView, getHorizontalHelper(layoutManager)!!)} else {out[0] = 0}if (layoutManager.canScrollVertically()) {out[1] = distanceToCenter(targetView, getVerticalHelper(layoutManager)!!)} else {out[1] = 0}return out}private fun distanceToCenter(targetView: View, helper: OrientationHelper): Int {Log.i(TAG, "distanceToCenter")// 计算目标ItemView的中心点(ItemView包含ItemDecoration的部分 + ItemView的宽度/高度的一半)的val childCenter = helper.getDecoratedStart(targetView) + helper.getDecoratedMeasurement(targetView) / 2// 计算RecyclerView的中心点(RecyclerView减去Padding的部分 + RecyclerView的宽度/高度的一半)val containerCenter = helper.startAfterPadding + helper.totalSpace / 2return childCenter - containerCenter}override fun findSnapView(layoutManager: RecyclerView.LayoutManager?): View? {Log.i(TAG, "findSnapView")if (layoutManager!!.canScrollVertically()) {return findCenterView(layoutManager, getVerticalHelper(layoutManager))} else if (layoutManager.canScrollHorizontally()) {return findCenterView(layoutManager, getHorizontalHelper(layoutManager))}return null}private fun findCenterView(layoutManager: RecyclerView.LayoutManager, helper: OrientationHelper): View? {Log.i(TAG, "findCenterView")val childCount = layoutManager.childCountif (childCount == 0) return null// 最接近RecyclerView中心的ItemViewvar closestItemView: View? = null// RecyclerView的中心点val center = helper.startAfterPadding + helper.totalSpace / 2var absClosest = Int.MAX_VALUEfor (i in 0 until childCount) {val child = layoutManager.getChildAt(i)// ItemView的中心点, 这里用Left是因为有ItemDecoration的存在,val childCenter = child?.left!! + helper.getDecoratedMeasurement(child) / 2val childDistance = Math.abs(childCenter - center)// 找到最靠近RecyclerView中心的ItemViewif (childDistance < absClosest) {absClosest = childDistanceclosestItemView = child}}return closestItemView}override fun findTargetSnapPosition(layoutManager: RecyclerView.LayoutManager?, velocityX: Int, velocityY: Int): Int {Log.i(TAG, "findTargetSnapPosition")// 判断layoutManager是否实现了RecyclerView.SmoothScroller.ScrollVectorProvider这个接口if (layoutManager !is RecyclerView.SmoothScroller.ScrollVectorProvider) return RecyclerView.NO_POSITION// 判断ItemView个数是否小于等于0val itemCount = layoutManager.itemCountif (itemCount == 0) return RecyclerView.NO_POSITION// 找到需要对齐的ItemViewval currentView = findSnapView(layoutManager) ?: return RecyclerView.NO_POSITION// 获取需要对齐ItemView的位置val currentPosition = layoutManager.getPosition(currentView)if (currentPosition == RecyclerView.NO_POSITION) return RecyclerView.NO_POSITION// 判断layoutManager的布局方向val vectorProvider = layoutManager as RecyclerView.SmoothScroller.ScrollVectorProviderval vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1) ?: return RecyclerView.NO_POSITION// 计算水平, 垂直方向最多能惯性滑动的ItemView个数,在当前ItemView的位置上,进行加减Position操作var maxHorizontalItemViewCount: Intvar maxVerticalItemViewCount: Intif (layoutManager.canScrollHorizontally()) {maxHorizontalItemViewCount = estimateNextPositionDiffForFling(layoutManager, getHorizontalHelper(layoutManager), velocityX, 0)var sign = Math.signum(velocityX.toFloat())if (sign == 0f) sign = 1f// 限制最多能惯性滑动的ItemView个数,限制滑动的 ItemView 的个数在 1 到 2 之间maxHorizontalItemViewCount = (sign * Math.min(Math.max(Math.abs(maxHorizontalItemViewCount), 0), 2)).toInt()if (vectorForEnd.x < 0) {maxHorizontalItemViewCount = - maxHorizontalItemViewCount}}else{maxHorizontalItemViewCount = 0}if (layoutManager.canScrollVertically()) {maxVerticalItemViewCount = estimateNextPositionDiffForFling(layoutManager, getVerticalHelper(layoutManager), 0, velocityY)var sign = Math.signum(velocityY.toFloat())if (sign == 0f) sign = 1f// 限制最多能惯性滑动的ItemView个数,限制滑动的 ItemView 的个数在 1 到 2 之间maxVerticalItemViewCount = (sign * Math.min(Math.max(Math.abs(maxVerticalItemViewCount), 0), 2)).toInt()if (vectorForEnd.y < 0) {maxVerticalItemViewCount = - maxVerticalItemViewCount}}else{maxVerticalItemViewCount = 0}// 根据滑动的方向,计算出最终的 ItemView 个数val finalItemCount = if(layoutManager.canScrollHorizontally()){maxHorizontalItemViewCount}else{maxVerticalItemViewCount}if (finalItemCount == 0) return RecyclerView.NO_POSITION// 确定最终的对齐位置,并做边界处理var targetPosition = currentPosition + finalItemCountif (targetPosition < 0) targetPosition = 0if (targetPosition >= layoutManager.itemCount) targetPosition = layoutManager.itemCount - 1return targetPosition}override fun calculateScrollDistance(velocityX: Int, velocityY: Int): IntArray {Log.i(TAG, "calculateScrollDistance")return super.calculateScrollDistance(velocityX, velocityY)}private fun estimateNextPositionDiffForFling(layoutManager: RecyclerView.LayoutManager, helper: OrientationHelper, velocityX: Int, velocityY: Int): Int {Log.i(TAG, "estimateNextPositionDiffForFling")val distances = calculateScrollDistance(velocityX, velocityY)val distancePerChild = computeDistancePerChild(layoutManager, helper)if (distancePerChild <= 0) return 0val distance = if (Math.abs(distances[0]) > Math.abs(distances[1])) distances[0] else distances[1]return Math.round(distance / distancePerChild)}private fun computeDistancePerChild(layoutManager: RecyclerView.LayoutManager, helper: OrientationHelper): Float {Log.i(TAG, "computeDistancePerChild")var minPositionView : View ?= nullvar maxPositionView : View ?= nullvar minPosition = Integer.MAX_VALUEvar maxPosition = Integer.MIN_VALUEval itemViewCount = layoutManager.childCountif (itemViewCount == 0) return INVALID_DISTANCE// 遍历所有ItemView, 找到最小位置和最大位置的ItemView,记录Positionfor (i in 0 until itemViewCount) {val child = layoutManager.getChildAt(i) ?: continueval position = layoutManager.getPosition(child)if (position == RecyclerView.NO_POSITION) continueif (position < minPosition) {minPosition = positionminPositionView = child}if (position > maxPosition) {maxPosition = positionmaxPositionView = child}}if (minPositionView == null || maxPositionView == null) return INVALID_DISTANCE// 计算最小位置和最大位置的ItemView离RecyclerView左边的距离val start = Math.min(helper.getDecoratedStart(minPositionView), helper.getDecoratedStart(maxPositionView))// 计算最小位置和最大位置的ItemView离RecyclerViewj右边的距离val end = Math.max(helper.getDecoratedEnd(minPositionView), helper.getDecoratedEnd(maxPositionView))// 计算最小位置和最大位置的ItemView的宽度val distance = end - startif (distance <= 0) return INVALID_DISTANCEreturn 1f * distance / (maxPosition - minPosition + 1)}private fun getVerticalHelper(layoutManager: RecyclerView.LayoutManager): OrientationHelper {Log.i(TAG, "getVerticalHelper")if (mVerticalHelper == null || mVerticalHelper?.layoutManager != layoutManager) {mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager)}return mVerticalHelper!!}private fun getHorizontalHelper(layoutManager: RecyclerView.LayoutManager): OrientationHelper {Log.i(TAG, "getHorizontalHelper")if (mHorizontalHelper == null || mHorizontalHelper!!.layoutManager != layoutManager) {mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager)}return mHorizontalHelper!!}
}

Activity代码

  • 第一次findSnapView:正常滑动停止后触发,需要找到对齐的View
  • 第二次findSnapView:惯性滑动停止后触发,需要找到对齐的View
const val TAG = "Yang"
class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val numberList = List(10){it}val layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)val mRv = findViewById<RecyclerView>(R.id.recyclerView)val mAdapter = MyAdapter(numberList)// 添加 ItemDecorationmRv.addItemDecoration(SpaceItemDecoration(dpToPx(this, 25f), dpToPx(this, 100f)))// 添加 LinearSnapHelperval linearSnapHelper = object : MySmoothSnapHelper() {override fun findSnapView(layoutManager: RecyclerView.LayoutManager?): View? {val snapView = super.findSnapView(layoutManager)val snapPosition = snapView?.let {mRv.getChildAdapterPosition(it) }snapPosition?.let {if (snapPosition != RecyclerView.NO_POSITION) {mAdapter.setSelectedPosition(snapPosition)}}return snapView}}linearSnapHelper.attachToRecyclerView(mRv)mRv?.layoutManager = layoutManagermRv?.adapter = mAdapter}fun dpToPx(context: Context, dp: Float): Int {val metrics = context.resources.displayMetricsreturn TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics).toInt()}
}// log
2024-06-21 01:18:42.794 17860-17860 Yang                    I  attachToRecyclerView
2024-06-21 01:18:45.412 17860-17860 Yang                    I  createScroller
2024-06-21 01:18:45.413 17860-17860 Yang                    I  findTargetSnapPosition
2024-06-21 01:18:45.413 17860-17860 Yang                    I  findSnapView
2024-06-21 01:18:45.413 17860-17860 Yang                    I  getHorizontalHelper
2024-06-21 01:18:45.413 17860-17860 Yang                    I  findCenterView
2024-06-21 01:18:45.413 17860-17860 Yang                    I  getHorizontalHelper
2024-06-21 01:18:45.413 17860-17860 Yang                    I  estimateNextPositionDiffForFling
2024-06-21 01:18:45.413 17860-17860 Yang                    I  calculateScrollDistance
2024-06-21 01:18:45.413 17860-17860 Yang                    I  computeDistancePerChild
2024-06-21 01:18:45.430 17860-17860 Yang                    I  onTargetFound
2024-06-21 01:18:45.430 17860-17860 Yang                    I  calculateDistanceToFinalSnap
2024-06-21 01:18:45.430 17860-17860 Yang                    I  getHorizontalHelper
2024-06-21 01:18:45.430 17860-17860 Yang                    I  distanceToCenter
2024-06-21 01:18:45.430 17860-17860 Yang                    I  calculateSpeedPerPixel
2024-06-21 01:18:46.400 17860-17860 Yang                    I  findSnapView
2024-06-21 01:18:46.400 17860-17860 Yang                    I  getHorizontalHelper
2024-06-21 01:18:46.400 17860-17860 Yang                    I  findCenterView
2024-06-21 01:18:46.400 17860-17860 Yang                    I  calculateDistanceToFinalSnap
2024-06-21 01:18:46.400 17860-17860 Yang                    I  getHorizontalHelper
2024-06-21 01:18:46.400 17860-17860 Yang                    I  distanceToCenter

效果图

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/859636.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一加12搞机(kernelsu+lsposed)

刷机 温馨提示&#xff1a;如果你不知道root的意义在哪&#xff0c;建议不要解锁和root&#xff0c;到时候救砖或者回锁都挺麻烦。 刷全量包 最新版的系统没有更新推送&#xff0c;所以去一加社区[0]找了个全量包来刷&#xff0c;。安装方式可以看帖子里的内容&#xff0c;说…

力扣-滑动窗口

文章目录 滑动窗口题目1-无重复字符的最长子串题目2-找到字符串中所有字母异位词 滑动窗口 滑动窗口是一种常用的算法技巧&#xff0c;适用于需要在一个数组或字符串中找出满足特定条件的连续子数组或子字符串的问题。它通过维护一个窗口范围来减少重复计算&#xff0c;从而优…

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据…

金融行业专题|某头部期货基于 K8s 原生存储构建自服务数据库云平台

为了进一步提升资源交付效率&#xff0c;不少用户都将数据库应用从物理环境迁移到容器环境。而对于 Kubernetes 部署环境&#xff0c;用户不仅需要考虑数据库在性能方面的需求&#xff0c;还要为数据存储提供更安全、可靠的高可用保障。 近期&#xff0c;某头部期货机构基于 S…

PaddleOCR C++源码编译以及demo测试

Windows10下使用PaddleOCRc 1.所需要的环境 PaddleOCR 源码文件&#xff1a;https://gitee.com/paddlepaddle/PaddleOCR &#xff08;本文选择2.6https://github.com/PaddlePaddle/PaddleOCR/archive/refs/tags/v2.6.0.zip&#xff09; opencv库&#xff1a;https://opencv…

PyTorch的环境配置和安装

PyTorch环境配置及安装 初步机器学习&#xff0c;这里记录下一些学习经过&#xff0c;之后以便于自己查看&#xff0c;同时欢迎各位大佬点评&#xff0c;本节是机器计算的一个包的安装和简单验证。 安装、使用环境 Windows环境下&#xff1a;CUDA官网使用IDM下载就很快乐&am…

LeetCode11. 盛最多水的容器题解

LeetCode11. 盛最多水的容器题解 题目链接&#xff1a; https://leetcode.cn/problems/container-with-most-water 示例 思路 暴力解法 定住一个柱子不动&#xff0c;然后用其他柱子与其围住面积&#xff0c;取最大值。 代码如下&#xff1a; public int maxArea1(int[]…

AI儿童绘本创作

之前分享过AI儿童绘画的项目&#xff0c;但是主要问题是角色一致要花费很长的时间&#xff01; 今天发现了这款&#xff0c;非常奈斯&#xff01; 只需输入故事主题、风格、模板&#xff0c;软件就会自动创作故事内容&#xff0c;自动生成插画配图&#xff0c;自动根据模板生…

Spring Bean自动装配:深入解析与实战应用

何为自动装配 在使用Spring框架配置bean时&#xff0c;我们通常需要为bean的属性设置值。如果不手动设置这些值&#xff0c;它们通常会被初始化为默认值&#xff08;对于对象类型通常是null&#xff0c;对于基本类型如int则是0&#xff0c;boolean是false等&#xff09;。自动…

(超详细)YOLOV7改进-Soft-NMS(支持多种IoU变种选择)

1.在until/general.py文件最后加上下面代码 2.在general.py里面找到这代码&#xff0c;修改这两个地方 3.之后直接运行即可

网页设计软件Bootstrap Studio6.7.1

Bootstrap Studio是一个适用于Windows的程序,允许您使用流行的fre***orca Bootstrap创建和原型网站。您可以将现成的组件拖动到工作区并直观地自定义它们。该程序生成干净和语义的PDF、CSS和JS代码,所有Web浏览器都支持这些代码。 Bootstrap Studio有一个漂亮而强大的界面,它…

DataWorks重磅推出全新资源组2.0,实现低成本灵活付费和动态平滑扩缩容

背景简介 DataWorks资源组为DataWorks上的各个功能模块提供计算资源&#xff0c;属于付费服务。 资源组属于DataWorks的基础组件&#xff0c;是客户正常使用DataWorks的前提。 资源组直接影响到相关功能是否正常运行&#xff0c;以及运行的效率和稳定性。 此前DataWorks资源…

ImportError: attempted relative import beyond top-level package报错,解决方法

1.如下图所示&#xff0c;在conftest.py页面采用相对路径去引用包&#xff0c;运行提示报错信息 目录结构: D:. ├─common ├─config ├─image ├─logFile ├─page │ └─basePage.py └─test_cases└─conftest.py从目录结构中我们可以看到conftest.py文件和basePage…

如何免费的去使用connectedpapers?

免费使用connectedpapers 1. 打开谷歌浏览器2. 按住ctrlshiftN,进入无痕模式3. 不需要登录&#xff08;也就是访客模式&#xff09;4. 两次用完&#xff0c;关闭无痕模式&#xff08;继续重复步骤 2 - 4&#xff09; 1. 打开谷歌浏览器 2. 按住ctrlshiftN,进入无痕模式 输入网…

使用Github API获取排名

看到有人使用Github 提供的API做了GitHub rank的网站&#xff0c;由于以前没有使用过Github的API&#xff0c;所以打算自己动手尝试一下。在线效果 1. Token 生成 使用API前需要先申请开发者Token&#xff0c;在Settings --> Developer settings --> Personal access t…

对的礼物送给对的人,送礼物的技巧和学问

一、教程描述 无论是商务往来&#xff0c;还是求人办事&#xff0c;送礼都是不可或缺的一种交际手段。想要建立好的人脉关系网&#xff0c;想要把事儿办成&#xff0c;一定要把对的礼物送给对的人&#xff0c;否则必是竹篮打水一场空。送礼是一门学问&#xff0c;同时&#xf…

使用babel将es6语法进行转换es2015

使用babel将es6语法进行转换es2015 1、npm初始化 npm init -y2、全局安装babel-cli 和 browserify npm i babel-cli browserify -g3、项目根目录下执行 npm i babel-preset-es2015 --save-dev4、项目根目录下新建.babelrc {"presets": ["es2015"] }5、…

【C语言】函数执行背后的秘密:函数栈帧的创建和销毁超详解

✨个人主页&#xff1a; 熬夜学编程的小林 &#x1f497;系列专栏&#xff1a; 【C语言详解】 【数据结构详解】 目录 1. 什么是函数栈帧 2. 理解函数栈帧能解决什么问题呢&#xff1f; 3. 函数栈帧的创建和销毁解析 3.1 什么是栈&#xff1f; 3.2 认识相关寄存器和汇编指…

讨论顺序表

讨论顺序表 C中的vector模拟实现成员变量尾插数据push_back扩容reserve 构造函数和析构函数拷贝构造函数指定位置插入数据指定位置删除数据迭代器失效完整代码 C中&#xff0c;vector是可以改变大小的数组的序列容器。可以看做底层就是一个数组&#xff0c;容量满时扩容。 C中的…

Android集成高德地图SDK(1)

1.新建Android应用&#xff0c;确定应用包名 2.注册高德开放平台&#xff0c;打开控制台页面&#xff0c;应用管理&#xff0c;我的应用&#xff0c;创建新应用 3.添加Key 4.获取SHA1码 找到Android Studio自带的keytool 将其拖到cmd中&#xff0c;输入命令 -v -list -keystor…