package com.doumee.keyCabinet.ui.view; import android.content.Context; import android.util.DisplayMetrics; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; public class RecyclerViewScrollHelper { public static void scrollToPosition(RecyclerView recyclerView, int position){ RecyclerView.LayoutManager manager1 = recyclerView.getLayoutManager(); if (manager1 instanceof LinearLayoutManager) { LinearLayoutManager manager = (LinearLayoutManager) manager1; final TopSmoothScroller mScroller = new TopSmoothScroller(recyclerView.getContext()); mScroller.setTargetPosition(position); manager.startSmoothScroll(mScroller); } } public static class TopSmoothScroller extends LinearSmoothScroller { TopSmoothScroller(Context context) { super(context); } @Override protected int getHorizontalSnapPreference() { return SNAP_TO_START; } @Override protected int getVerticalSnapPreference() { return SNAP_TO_START; } @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return super.calculateSpeedPerPixel(displayMetrics)*0.5f; } } }