Skip to content

Commit 56f20d5

Browse files
committed
chore: opt LayoutManager code
1 parent d3cbf5a commit 56f20d5

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

library/src/main/java/io/iftech/android/library/square/SquareLayoutManager.kt

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

library/src/main/java/io/iftech/android/library/square/SquareSnapHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlin.math.max
1010
import kotlin.math.min
1111
import kotlin.math.roundToInt
1212

13-
class SquareSnapHelper : RecyclerView.OnFlingListener() {
13+
internal class SquareSnapHelper : RecyclerView.OnFlingListener() {
1414

1515
private var mVerticalHelper: OrientationHelper? = null
1616
private var mHorizontalHelper: OrientationHelper? = null

0 commit comments

Comments
 (0)