Created
January 18, 2017 10:03
-
-
Save sugoi-wada/b7a28573d019aff0c50422dad27e4a1d to your computer and use it in GitHub Desktop.
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
import android.content.Context | |
import android.graphics.PointF | |
import android.support.v7.widget.LinearLayoutManager | |
import android.support.v7.widget.LinearSmoothScroller | |
import android.support.v7.widget.RecyclerView | |
class SmoothScrollableLinearLayoutManager @JvmOverloads constructor(context: Context, orientation: Int = LinearLayoutManager.VERTICAL, reverseLayout: Boolean = false) : LinearLayoutManager(context, orientation, reverseLayout) { | |
override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int) { | |
startSmoothScroll(TopSnappedSmoothScroller(recyclerView.context).apply { | |
targetPosition = position | |
}) | |
} | |
private inner class TopSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) { | |
override fun computeScrollVectorForPosition(targetPosition: Int): PointF { | |
return this@SmoothScrollableLinearLayoutManager.computeScrollVectorForPosition(targetPosition) | |
} | |
override fun getVerticalSnapPreference(): Int = SNAP_TO_START | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment