I have a RecyclerView
in which i have an image ,a few TextView
s and 2 ImageButton
s.
I have 7-8 such rows to display in my Activity
I had the same problem. Here is the solution to slow down the fling.
val maxFling=4000
onFlingListener = object : RecyclerView.OnFlingListener() {
override fun onFling(velocityX: Int, velocityY: Int): Boolean {
if (velocityY > maxFling) {
fling(velocityX, maxFling)
return true
} else if (velocityY < -maxFling) {
fling(velocityX, -maxFling)
return true
} else {
return false
}
}
}
There is one RecyclerView
method specifically designed to control the fling speed you only need to override it to achieve the friction/slowdown effect you mentioned in your question.
One simple implementation you could try:
@Override
public boolean fling(int velocityX, int velocityY)
{
// if FLING_SPEED_FACTOR between [0, 1[ => slowdown
velocityY *= FLING_SPEED_FACTOR;
return super.fling(velocityX, velocityY);
}
RecyclerView is great in that it is super modular. you can set your custom scroller to the LayoutMananger
You can use https://github.com/apptik/MultiView/tree/master/scrollers
Example:
RecyclerView.SmoothScroller smoothScroller = new FlexiSmoothScroller(getContext())
.setMillisecondsPerInchSearchingTarget(100f));
smoothScroller.setTargetPosition(targetPos);
recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
you can check more examples at: https://github.com/apptik/MultiView/blob/master/app/src/main/java/io/apptik/multiview/ScrollersFragment.java
to control your recycler view's scroll speed See this SO post: Snappy scrolling in RecyclerView
Basically you could override recyclerviews smoothScroll, or implement your own onGestureListener and attach it to the recyclerView
This works nicely to put a maximum threshold on the fling speed:
mRecyclerView.setOnFlingListener(new RecyclerView.OnFlingListener() {
@Override
public boolean onFling(int velocityX, int velocityY) {
if (Math.abs(velocityY) > MAX_VELOCITY_Y) {
velocityY = MAX_VELOCITY_Y * (int) Math.signum((double)velocityY);
mRecyclerView.fling(velocityX, velocityY);
return true;
}
return false;
}
});