Created
April 28, 2019 01:37
-
-
Save CodeK1988/711de815af0a035d36775313e8df4c55 to your computer and use it in GitHub Desktop.
SnapOnScrollListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SnapOnScrollListener( | |
private val snapHelper: SnapHelper, | |
var onSnapPositionChangeListener: Utils.OnSnapPositionChangeListener? = null | |
) : RecyclerView.OnScrollListener() { | |
private var snapPosition = RecyclerView.NO_POSITION | |
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { | |
/*val snapPosition = snapHelper.getSnapPosition(recyclerView) | |
val snapPositionChanged = this.snapPosition != snapPosition | |
if (snapPositionChanged) { | |
onSnapPositionChangeListener?.onScrolled(snapPosition) | |
this.snapPosition = snapPosition | |
}*/ | |
} | |
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { | |
if (newState == RecyclerView.SCROLL_STATE_IDLE) { | |
updatePos(recyclerView) | |
} else if(newState == RecyclerView.SCROLL_STATE_DRAGGING){ | |
// onSnapPositionChangeListener?.onScrolled() | |
} | |
} | |
private fun updatePos(recyclerView: RecyclerView) { | |
val snapPosition = snapHelper.getSnapPosition(recyclerView) | |
val snapPositionChanged = this.snapPosition != snapPosition | |
if (snapPositionChanged) { | |
onSnapPositionChangeListener?.onSnapPositionChange(snapPosition) | |
this.snapPosition = snapPosition | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment