(Smooth)ScrollToPosition doesn't work properly

2019-01-14 10:41发布

I'm using basic RecyclerView with GridLayoutManager. I observed that nor smoothScrollToPosition nor scrollToPosition works properly.

a) when using smoothScrollToPosition I often receive error from RecyclerView

"RecyclerView﹕ Passed over target position while smooth scrolling."

and RecyclerView is not scrolled properly (often it misses the targeted row). This is observed mostly when I'm trying to scroll to the 1st item of some row

b) when using scrollToPosition it seems to work quite ok but most of the time I can see only the 1st item of the row and the rest are not displayed.

Can you give me some hints how to make work properly at least one of the methods?

Thanks a lot!

8条回答
三岁会撩人
2楼-- · 2019-01-14 10:58

when you use scrollToPosition it will show it on top of the recycler view.

But if you use smoothScrollToPosition it will scroll till it come in to Window Visible. that's why while smoothScrool to item below, it will show it on bottom

查看更多
爷的心禁止访问
3楼-- · 2019-01-14 11:04

Calling the recyclerView smoothScroll isn't effective, as the recyclerView itself doesn't handle its layout.

What you should do is calling the layout manager scroll method instead.

This should look something like this

mRecyclerView.getLayoutManager().scrollToPosition(desiredPosition);
查看更多
仙女界的扛把子
4楼-- · 2019-01-14 11:11

Try measuring item width or height and call smoothScrollBy(int dx, int dy).

查看更多
Lonely孤独者°
5楼-- · 2019-01-14 11:13

In My case,

`mRecyclerView.scrollToPosition(10);` 

also did not work. But

`mRecyclerView.smoothScrollToPosition(10);` 

works fine for me...

查看更多
对你真心纯属浪费
6楼-- · 2019-01-14 11:19

Finally I was able to make it work! LinearLayoutManager.scrollToPositionWithOffset(int, int) did the trick.

查看更多
走好不送
7楼-- · 2019-01-14 11:20

I also have same issue, but managed to fix the issue by Customizing SmoothScroller

let Custom LayoutManager as below

public class CustomLayoutManager extends LinearLayoutManager {
    private static final float MILLISECONDS_PER_INCH = 50f;
    private Context mContext;

    public CustomLayoutManager(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView,
        RecyclerView.State state, final int position) {

        LinearSmoothScroller smoothScroller = 
            new LinearSmoothScroller(mContext) {

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

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

        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }
}

(documentation commented inside the code given above)Please set the above LayoutManager to the recyerview

CustomLayoutManagerlayoutManager = new CustomLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.smoothScrollToPosition(position);

by using the custom Layout manager

scrollToPosition also working well in my case u can use

recyclerView.scrollToPosition(position)

also if you want to adjust the speed of smoothScrollToPosition please override the

private static final float MILLISECONDS_PER_INCH = 50f;

in CustomLayoutManager. So if we put the value as 1f the smoothScrollToPosition will be faster like scrollToPosition.increasing value make delay and decreasing will make the speed of scroll. Hope this will useful.

查看更多
登录 后发表回答