@@ -51,18 +51,46 @@ class SquareLayoutManager @JvmOverloads constructor(
5151 private val horizontalMinOffset: Float
5252 get() = if (childWidth == 0 ) 0f else (width - childWidth) / 2f
5353
54- var isAutoSelect = true
5554 private var selectAnimator: ValueAnimator ? = null
5655
5756 /* *
58- * 初始化布局的时候,是否是定位到中心的 item
57+ * 使用 SnapHelper 自动选中最靠近中心的 Item,默认为true
58+ */
59+ var isAutoSelect = true
60+
61+ /* *
62+ * 初始化布局的时候,是否定位到中心的 Item
5963 */
6064 var isInitLayoutCenter = true
6165 private var isFirstLayout = true
6266
6367 private var lastSelectedPosition = 0
6468 private var onItemSelectedListener: (Int ) -> Unit = {}
6569
70+ /* *
71+ * 滑动到指定位置
72+ */
73+ fun smoothScrollToPosition (position : Int ) {
74+ if (position > - 1 && position < itemCount) {
75+ startValueAnimator(position)
76+ }
77+ }
78+
79+ /* *
80+ * 滑动到中心
81+ */
82+ fun smoothScrollToCenter () {
83+ val centerPos = rowCount / 2 * spanCount + spanCount / 2
84+ smoothScrollToPosition(centerPos)
85+ }
86+
87+ /* *
88+ * 设置选中 Item 的监听回调
89+ */
90+ fun setOnItemSelectedListener (listener : (Int ) -> Unit ) {
91+ onItemSelectedListener = listener
92+ }
93+
6694 override fun generateDefaultLayoutParams (): RecyclerView .LayoutParams {
6795 return RecyclerView .LayoutParams (
6896 RecyclerView .LayoutParams .WRAP_CONTENT ,
@@ -327,17 +355,6 @@ class SquareLayoutManager @JvmOverloads constructor(
327355 return childWidth * (targetPos % spanCount) - horizontalOffset.toInt()
328356 }
329357
330- fun smoothScrollToPosition (position : Int ) {
331- if (position > - 1 && position < itemCount) {
332- startValueAnimator(position)
333- }
334- }
335-
336- fun smoothScrollToCenter () {
337- val centerPos = rowCount / 2 * spanCount + spanCount / 2
338- smoothScrollToPosition(centerPos)
339- }
340-
341358 override fun smoothScrollToPosition (
342359 recyclerView : RecyclerView ? ,
343360 state : RecyclerView .State ? ,
@@ -346,9 +363,6 @@ class SquareLayoutManager @JvmOverloads constructor(
346363 smoothScrollToPosition(position)
347364 }
348365
349- fun setOnItemSelectedListener (listener : (Int ) -> Unit ) {
350- onItemSelectedListener = listener
351- }
352366
353367 private fun startValueAnimator (position : Int ) {
354368 cancelAnimator()
0 commit comments