diff --git a/StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/StackLayoutManager.kt b/StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/StackLayoutManager.kt index a3d1fb5..b41be73 100644 --- a/StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/StackLayoutManager.kt +++ b/StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/StackLayoutManager.kt @@ -160,13 +160,25 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation, return mScrollOrientation } + /** + * 返回第一个可见 itemView 的位置. + * @return 返回第一个可见 itemView 的位置. + */ + fun getFirstVisibleItemPosition(): Int { + return when(mScrollOrientation) { + ScrollOrientation.RIGHT_TO_LEFT -> Math.floor((mScrollOffset * 1.0 / width)).toInt() + ScrollOrientation.LEFT_TO_RIGHT -> itemCount - 1 - Math.ceil((mScrollOffset * 1.0 / width)).toInt() + ScrollOrientation.BOTTOM_TO_TOP -> Math.floor((mScrollOffset * 1.0 / height)).toInt() + ScrollOrientation.TOP_TO_BOTTOM -> itemCount - 1 - Math.ceil((mScrollOffset * 1.0 / height)).toInt() + } + } + constructor(scrollOrientation: ScrollOrientation) : this(scrollOrientation, 3, DefaultAnimation::class.java, DefaultLayout::class.java) constructor(scrollOrientation: ScrollOrientation, visibleCount: Int) : this(scrollOrientation, visibleCount, DefaultAnimation::class.java, DefaultLayout::class.java) constructor() : this(ScrollOrientation.RIGHT_TO_LEFT) - override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams { return RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) @@ -383,15 +395,6 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation, } } - private fun getFirstVisibleItemPosition(): Int { - return when(mScrollOrientation) { - ScrollOrientation.RIGHT_TO_LEFT -> Math.floor((mScrollOffset * 1.0 / width)).toInt() - ScrollOrientation.LEFT_TO_RIGHT -> itemCount - 1 - Math.ceil((mScrollOffset * 1.0 / width)).toInt() - ScrollOrientation.BOTTOM_TO_TOP -> Math.floor((mScrollOffset * 1.0 / height)).toInt() - ScrollOrientation.TOP_TO_BOTTOM -> itemCount - 1 - Math.ceil((mScrollOffset * 1.0 / height)).toInt() - } - } - private fun getLastVisibleItemPosition(): Int { val firstVisiblePosition = getFirstVisibleItemPosition() return if (firstVisiblePosition + mVisibleItemCount > itemCount - 1) {