mirror of
https://github.com/leaf-wai/StackLayoutManager.git
synced 2026-07-12 10:51:17 +08:00
Supports getting the position of first visible item
Supports getting the position of first visible item
This commit is contained in:
@@ -160,13 +160,25 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
|
|||||||
return mScrollOrientation
|
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) : this(scrollOrientation, 3, DefaultAnimation::class.java, DefaultLayout::class.java)
|
||||||
|
|
||||||
constructor(scrollOrientation: ScrollOrientation, visibleCount: Int) : this(scrollOrientation, visibleCount, 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)
|
constructor() : this(ScrollOrientation.RIGHT_TO_LEFT)
|
||||||
|
|
||||||
|
|
||||||
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
|
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
|
||||||
return RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
|
return RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||||
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 {
|
private fun getLastVisibleItemPosition(): Int {
|
||||||
val firstVisiblePosition = getFirstVisibleItemPosition()
|
val firstVisiblePosition = getFirstVisibleItemPosition()
|
||||||
return if (firstVisiblePosition + mVisibleItemCount > itemCount - 1) {
|
return if (firstVisiblePosition + mVisibleItemCount > itemCount - 1) {
|
||||||
|
|||||||
Reference in New Issue
Block a user