[bugfix]
[recycleview 居中问题]
This commit is contained in:
@@ -30,6 +30,7 @@ import com.mogo.och.bus.passenger.R
|
||||
import com.mogo.och.bus.passenger.bean.Temperature
|
||||
import com.mogo.och.bus.passenger.presenter.BusPassengerFunctionSoftPresenter
|
||||
import com.mogo.och.bus.passenger.ui.adapter.TemperatureAdapter
|
||||
import com.mogo.och.bus.passenger.ui.layoutmanage.PagerCenterSnapHelper
|
||||
import com.mogo.och.bus.passenger.view.HorizontalDecoration
|
||||
import com.mogo.och.bus.passenger.view.PickerLayoutManager
|
||||
import com.mogo.och.common.module.utils.SoundPoolHelper
|
||||
@@ -235,12 +236,12 @@ class M1SoftFragment :
|
||||
|
||||
val data = getData()
|
||||
val adapter = TemperatureAdapter(requireContext(), data, rv_aircondition_temperature)
|
||||
val snapHelper: SnapHelper = LinearSnapHelper()
|
||||
val snapHelper = PagerCenterSnapHelper()
|
||||
snapHelper.attachToRecyclerView(rv_aircondition_temperature)
|
||||
rv_aircondition_temperature.layoutManager = pickerLayoutManager
|
||||
rv_aircondition_temperature.adapter = adapter
|
||||
HorizontalDecoration.distance = SharedPrefsMgr.getInstance(requireContext()).getInt(HorizontalDecoration.distancekey,0)
|
||||
val space = AutoSizeUtils.dp2px(context, 22f)
|
||||
val space = AutoSizeUtils.dp2px(context, 15f)
|
||||
rv_aircondition_temperature.addItemDecoration(HorizontalDecoration(space,data.size-1))
|
||||
|
||||
pickerLayoutManager.setOnScrollStopListener { view ->
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.mogo.och.bus.passenger.ui.layoutmanage
|
||||
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.OrientationHelper
|
||||
import androidx.recyclerview.widget.PagerSnapHelper
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
class PagerCenterSnapHelper : PagerSnapHelper() {
|
||||
private var mHorizontalHelper: OrientationHelper? = null
|
||||
private var pageScollListener: PageScollListener? = null
|
||||
override fun findSnapView(layoutManager: RecyclerView.LayoutManager): View? {
|
||||
if (layoutManager.canScrollVertically()) {
|
||||
return super.findSnapView(layoutManager)
|
||||
} else if (layoutManager.canScrollHorizontally()) {
|
||||
return findCenterView(layoutManager, getHorizontalHelper(layoutManager))
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun findCenterView(
|
||||
layoutManager: RecyclerView.LayoutManager,
|
||||
helper: OrientationHelper
|
||||
): View? {
|
||||
val childCount = layoutManager.childCount
|
||||
if (childCount == 0) {
|
||||
return null
|
||||
}
|
||||
var closestChild: View? = null
|
||||
val center = helper.startAfterPadding + helper.totalSpace / 2
|
||||
var absClosest = Int.MAX_VALUE
|
||||
for (i in 0 until childCount) {
|
||||
val child = layoutManager.getChildAt(i)
|
||||
val params = child!!.layoutParams as RecyclerView.LayoutParams
|
||||
var childCenter = 0
|
||||
childCenter = if (i == 0) {
|
||||
helper.getDecoratedStart(child) + helper.getDecoratedMeasurement(child) - layoutManager.getDecoratedMeasuredWidth(
|
||||
child
|
||||
) / 2 - params.rightMargin / 2
|
||||
} else if (i == childCount - 1) {
|
||||
(helper.getDecoratedStart(child)
|
||||
+ (helper.getDecoratedMeasurement(child) - layoutManager.getDecoratedMeasuredWidth(
|
||||
child
|
||||
) / 2 - params.leftMargin / 2 - params.rightMargin))
|
||||
} else {
|
||||
helper.getDecoratedStart(child) + helper.getDecoratedMeasurement(child) / 2
|
||||
}
|
||||
val absDistance = Math.abs(childCenter - center)
|
||||
/* if child center is closer than previous closest, set it as closest */if (absDistance < absClosest) {
|
||||
absClosest = absDistance
|
||||
closestChild = child
|
||||
}
|
||||
}
|
||||
if (pageScollListener != null) {
|
||||
if (closestChild != null) {
|
||||
pageScollListener!!.onPageSelected(layoutManager.getPosition(closestChild))
|
||||
}
|
||||
}
|
||||
return closestChild
|
||||
}
|
||||
|
||||
private fun getHorizontalHelper(
|
||||
layoutManager: RecyclerView.LayoutManager
|
||||
): OrientationHelper {
|
||||
if (mHorizontalHelper == null || mHorizontalHelper!!.layoutManager !== layoutManager) {
|
||||
mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager)
|
||||
}
|
||||
return mHorizontalHelper!!
|
||||
}
|
||||
|
||||
fun setPageScollListener(pageScollListener: PageScollListener?) {
|
||||
this.pageScollListener = pageScollListener
|
||||
}
|
||||
|
||||
interface PageScollListener {
|
||||
fun onPageSelected(i: Int)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "PagerSnapHelper "
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user