mirror of
https://github.com/leaf-wai/StackLayoutManager.git
synced 2026-07-13 11:21:16 +08:00
Avoid divide by zero
This commit is contained in:
@@ -2,7 +2,8 @@ package com.littlemango.stacklayoutmanager
|
|||||||
|
|
||||||
import android.support.annotation.IntRange
|
import android.support.annotation.IntRange
|
||||||
import android.support.v7.widget.RecyclerView
|
import android.support.v7.widget.RecyclerView
|
||||||
import android.support.v7.widget.RecyclerView.*
|
import android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING
|
||||||
|
import android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
|
||||||
@@ -153,6 +154,9 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
|
|||||||
* @return 返回第一个可见 itemView 的位置.
|
* @return 返回第一个可见 itemView 的位置.
|
||||||
*/
|
*/
|
||||||
fun getFirstVisibleItemPosition(): Int {
|
fun getFirstVisibleItemPosition(): Int {
|
||||||
|
if (width == 0 || height == 0) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return when (mScrollOrientation) {
|
return when (mScrollOrientation) {
|
||||||
ScrollOrientation.RIGHT_TO_LEFT -> Math.floor((mScrollOffset * 1.0 / width)).toInt()
|
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.LEFT_TO_RIGHT -> itemCount - 1 - Math.ceil((mScrollOffset * 1.0 / width)).toInt()
|
||||||
@@ -264,6 +268,9 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun canScrollHorizontally(): Boolean {
|
override fun canScrollHorizontally(): Boolean {
|
||||||
|
if (itemCount == 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return when (mScrollOrientation) {
|
return when (mScrollOrientation) {
|
||||||
ScrollOrientation.LEFT_TO_RIGHT, ScrollOrientation.RIGHT_TO_LEFT -> true
|
ScrollOrientation.LEFT_TO_RIGHT, ScrollOrientation.RIGHT_TO_LEFT -> true
|
||||||
else -> false
|
else -> false
|
||||||
@@ -271,6 +278,9 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun canScrollVertically(): Boolean {
|
override fun canScrollVertically(): Boolean {
|
||||||
|
if (itemCount == 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return when (mScrollOrientation) {
|
return when (mScrollOrientation) {
|
||||||
ScrollOrientation.TOP_TO_BOTTOM, ScrollOrientation.BOTTOM_TO_TOP -> true
|
ScrollOrientation.TOP_TO_BOTTOM, ScrollOrientation.BOTTOM_TO_TOP -> true
|
||||||
else -> false
|
else -> false
|
||||||
@@ -278,14 +288,16 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
|
override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
|
||||||
|
|
||||||
mLayout?.requestLayout()
|
mLayout?.requestLayout()
|
||||||
|
|
||||||
removeAndRecycleAllViews(recycler)
|
removeAndRecycleAllViews(recycler)
|
||||||
|
|
||||||
|
if (itemCount > 0) {
|
||||||
mScrollOffset = getValidOffset(mScrollOffset)
|
mScrollOffset = getValidOffset(mScrollOffset)
|
||||||
|
|
||||||
loadItemView(recycler)
|
loadItemView(recycler)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int {
|
override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int {
|
||||||
return handleScrollBy(dx, recycler)
|
return handleScrollBy(dx, recycler)
|
||||||
@@ -439,6 +451,9 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getFirstVisibleItemMovePercent(): Float {
|
private fun getFirstVisibleItemMovePercent(): Float {
|
||||||
|
if (width == 0 || height == 0) {
|
||||||
|
return 0f
|
||||||
|
}
|
||||||
return when (mScrollOrientation) {
|
return when (mScrollOrientation) {
|
||||||
ScrollOrientation.RIGHT_TO_LEFT -> (mScrollOffset % width) * 1.0f / width
|
ScrollOrientation.RIGHT_TO_LEFT -> (mScrollOffset % width) * 1.0f / width
|
||||||
ScrollOrientation.LEFT_TO_RIGHT -> {
|
ScrollOrientation.LEFT_TO_RIGHT -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user