如何在Recyclerview / Layoutmanager中获得Scrollposition? [英] How to get the Scrollposition in the Recyclerview/Layoutmanager?

查看:224
本文介绍了如何在Recyclerview / Layoutmanager中获得Scrollposition?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Recyclerview Layoutmanager 中获取滚动位置?

How to get the scrollposition in Recyclerview or the Layoutmanager?

我可以通过添加 OnScrollListener 来测量滚动位置,但是当Orientation更改时,scrollDy 为0。

I can measure the scrollposition by adding an OnScrollListener, but when the Orientation changes scrollDy is 0.

    mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int scrollDy = 0;
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                scrollDy += dy;
            }
        });


推荐答案

你无法得到它,因为它确实不存在。 LayoutManager 只知道屏幕上的视图,它不知道之前的视图,它们的大小等等。

You cannot get it because it does not really exist. LayoutManager only knows about the views on screen, it does not know the views before, what their size is etc.

使用滚动侦听器可以计算的数字是不可靠的,因为如果数据发生更改, RecyclerView 将执行新的布局计算,并且不会尝试重新计算实际偏移量(如果移动了视图,你将收到 onScroll(0,0)

The number you can count using the scroll listener is not reliable because if data changes, RecyclerView will do a fresh layout calculation and will not try to re-calculate a real offset (you'll receive an onScroll(0, 0) if views moved).

RecyclerView估算滚动条的这个值,您可以使用查看类中的相同方法。

RecyclerView estimates this value for scrollbars, you can use the same methods from View class.

computeHorizo​​ntalScrollExtent

computeHorizo​​ntalScrollRange

computeHorizo​​ntalScrollOffset

这些方法也有垂直对应方式。

These methods have their vertical counterparts as well.

这篇关于如何在Recyclerview / Layoutmanager中获得Scrollposition?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆