Avoid divide by zero

This commit is contained in:
Christophe Smet
2018-10-30 14:27:56 +01:00
parent da9fbb3106
commit 5740def0f9

View File

@@ -2,7 +2,8 @@ package com.littlemango.stacklayoutmanager
import android.support.annotation.IntRange
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.ViewGroup
@@ -153,6 +154,9 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
* @return 返回第一个可见 itemView 的位置.
*/
fun getFirstVisibleItemPosition(): Int {
if (width == 0 || height == 0) {
return 0
}
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()
@@ -264,6 +268,9 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
}
override fun canScrollHorizontally(): Boolean {
if (itemCount == 0) {
return false
}
return when (mScrollOrientation) {
ScrollOrientation.LEFT_TO_RIGHT, ScrollOrientation.RIGHT_TO_LEFT -> true
else -> false
@@ -271,6 +278,9 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
}
override fun canScrollVertically(): Boolean {
if (itemCount == 0) {
return false
}
return when (mScrollOrientation) {
ScrollOrientation.TOP_TO_BOTTOM, ScrollOrientation.BOTTOM_TO_TOP -> true
else -> false
@@ -278,14 +288,16 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
}
override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
mLayout?.requestLayout()
removeAndRecycleAllViews(recycler)
if (itemCount > 0) {
mScrollOffset = getValidOffset(mScrollOffset)
loadItemView(recycler)
}
}
override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int {
return handleScrollBy(dx, recycler)
@@ -439,6 +451,9 @@ class StackLayoutManager(scrollOrientation: ScrollOrientation,
}
private fun getFirstVisibleItemMovePercent(): Float {
if (width == 0 || height == 0) {
return 0f
}
return when (mScrollOrientation) {
ScrollOrientation.RIGHT_TO_LEFT -> (mScrollOffset % width) * 1.0f / width
ScrollOrientation.LEFT_TO_RIGHT -> {