1

I'm tring to make smooth scroller to need position in recyclerView. I'm ovverride method smoothScrollToPosition in LinearLayoutManager, and it's work fine. But, I need to set scrolled position on top of screen(if is possible). I also tried scrollToPositionWithOffset, and it's make that I need, but now, without smooth effect. How to mix this methods, and make smooth scroll with set item on top?

private LinearLayoutManager linearLayoutManager = new    LinearLayoutManager(this){
    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView,
                                       RecyclerView.State state, final int position) {

        LinearSmoothScroller smoothScroller =
                new LinearSmoothScroller(getApplicationContext()) {

                    //This controls the direction in which smoothScroll looks
                    //for your view
                    @Override
                    public PointF computeScrollVectorForPosition
                    (int targetPosition) {
                        return this
                                .computeScrollVectorForPosition(targetPosition);
                    }

                    //This returns the milliseconds it takes to
                    //scroll one pixel.
                    @Override
                    protected float calculateSpeedPerPixel
                    (DisplayMetrics displayMetrics) {
                        return 50f/displayMetrics.densityDpi;
                    }
                };

        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }
};
  • Sounds similar to this http://stackoverflow.com/questions/7202193/scroll-up-a-scrollview-slowly/27028101#27028101 Maybe the same approach can be used here – Muzikant Sep 08 '16 at 11:05

1 Answers1

4

This is late response, but I want to suggest a library SnappySmoothScroller. This library enables smooth scroll with specific snap position: SnapType.START , SnapType.CENTER or SnapType.END

Use like this:

    private LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
            SnappySmoothScroller scroller = new SnappySmoothScroller.Builder()
                    .setPosition(position)
                    .setSnapType(SnapType.START)
                    .setScrollVectorDetector(new LinearLayoutScrollVectorDetector(this))
                    .build(recyclerView.getContext());

            startSmoothScroll(scroller);
        }
    };
nshmura
  • 5,940
  • 3
  • 27
  • 46