Created
August 29, 2017 03:07
-
-
Save maxcruz/d289a8e68ba15647c9fed6fa95361901 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
package com.mercadoni.user.android.faq | |
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 | |
import android.util.DisplayMetrics | |
/** | |
* | |
*/ | |
class TopScrollLinearLayoutManager(context: Context) : LinearLayoutManager(context) { | |
companion object { | |
const val MILLISECONDS_PER_INCH = 50f | |
} | |
override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?, | |
position: Int) { | |
val smoothScroller = TopSnappedSmoothScroller(recyclerView.context) | |
smoothScroller.targetPosition = position | |
startSmoothScroll(smoothScroller) | |
} | |
private inner class TopSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) { | |
override fun computeScrollVectorForPosition(targetPosition: Int): PointF? { | |
return this@TopScrollLinearLayoutManager | |
.computeScrollVectorForPosition(targetPosition) | |
} | |
override fun getVerticalSnapPreference(): Int { | |
return SNAP_TO_START | |
} | |
override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float { | |
if (displayMetrics == null) return super.calculateSpeedPerPixel(displayMetrics) | |
else return MILLISECONDS_PER_INCH / displayMetrics.densityDpi | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment