Supports getting the position of first visible item

Supports getting the position of first visible item
This commit is contained in:
Little Mango
2018-07-04 11:02:44 +08:00
parent 10f7f3ea23
commit 14c1021530

View File

@ -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) {