滚动RecyclerView(OnscrollListener)时滞后/错误的y坐标

时间:2016-01-13 17:50:47

标签: java android android-actionbar android-recyclerview toolbar

我正在使用RecyclerView.OnscrollListener和onscrolled-Method来解决滞后问题。 (当我滚动列表时,我想隐藏工具栏/操作栏 - 没有协调器 - 布局)

当我滚动"快速"时,我从onScrolled-Method收到的数字是正确的。但是当我慢慢地滚动我的列表时,我会收到一些"跳跃"在y轴上。

这是一个已知问题还是我做错了什么? 有没有人有这方面的经验?

使用过的侦听器如下: https://github.com/mzgreen/HideOnScrollExample/blob/master/app/src/main/java/pl/michalz/hideonscrollexample/listener/parttwo/HidingScrollListener.java

我在Android Studio中获得的日志(你可以在哪里看到跳转)是:

    01-13 17:21:33.611 D/Ilias: Offset: 0 *** dx: 0 *** dy: 9 *** totalScrolled: 0
    01-13 17:21:33.631 D/Ilias: Offset: 9 *** dx: 0 *** dy: 28 *** totalScrolled: 9
    01-13 17:21:33.641 D/Ilias: Offset: 37 *** dx: 0 *** dy: -5 *** totalScrolled: 37
    01-13 17:21:33.691 D/Ilias: Offset: 32 *** dx: 0 *** dy: -22 *** totalScrolled: 32
    01-13 17:21:33.701 D/Ilias: Offset: 10 *** dx: 0 *** dy: 10 *** totalScrolled: 10
    01-13 17:21:33.711 D/Ilias: Offset: 20 *** dx: 0 *** dy: 29 *** totalScrolled: 20
    01-13 17:21:33.731 D/Ilias: Offset: 49 *** dx: 0 *** dy: -2 *** totalScrolled: 49
    01-13 17:21:33.751 D/Ilias: Offset: 47 *** dx: 0 *** dy: -19 *** totalScrolled: 47
    01-13 17:21:33.761 D/Ilias: Offset: 28 *** dx: 0 *** dy: 11 *** totalScrolled: 28
    01-13 17:21:33.781 D/Ilias: Offset: 39 *** dx: 0 *** dy: 28 *** totalScrolled: 39
    01-13 17:21:33.801 D/Ilias: Offset: 67 *** dx: 0 *** dy: -4 *** totalScrolled: 67
    01-13 17:21:33.811 D/Ilias: Offset: 63 *** dx: 0 *** dy: -23 *** totalScrolled: 63
    01-13 17:21:33.831 D/Ilias: Offset: 40 *** dx: 0 *** dy: 7 *** totalScrolled: 40
    01-13 17:21:33.851 D/Ilias: Offset: 47 *** dx: 0 *** dy: 25 *** totalScrolled: 47
    01-13 17:21:33.861 D/Ilias: Offset: 72 *** dx: 0 *** dy: -7 *** totalScrolled: 72
    01-13 17:21:33.881 D/Ilias: Offset: 65 *** dx: 0 *** dy: -24 *** totalScrolled: 65
    01-13 17:21:33.901 D/Ilias: Offset: 41 *** dx: 0 *** dy: 9 *** totalScrolled: 41
    01-13 17:21:33.921 D/Ilias: Offset: 50 *** dx: 0 *** dy: 26 *** totalScrolled: 50
    01-13 17:21:33.931 D/Ilias: Offset: 76 *** dx: 0 *** dy: -8 *** totalScrolled: 76
    01-13 17:21:33.951 D/Ilias: Offset: 68 *** dx: 0 *** dy: -24 *** totalScrolled: 68
    01-13 17:21:33.971 D/Ilias: Offset: 44 *** dx: 0 *** dy: 9 *** totalScrolled: 44
    01-13 17:21:33.981 D/Ilias: Offset: 53 *** dx: 0 *** dy: 26 *** totalScrolled: 53
    01-13 17:21:34.001 D/Ilias: Offset: 79 *** dx: 0 *** dy: -6 *** totalScrolled: 79
    01-13 17:21:34.021 D/Ilias: Offset: 73 *** dx: 0 *** dy: -23 *** totalScrolled: 73
    01-13 17:21:34.031 D/Ilias: Offset: 50 *** dx: 0 *** dy: 11 *** totalScrolled: 50
    01-13 17:21:34.051 D/Ilias: Offset: 61 *** dx: 0 *** dy: 29 *** totalScrolled: 61
    01-13 17:21:34.071 D/Ilias: Offset: 90 *** dx: 0 *** dy: -5 *** totalScrolled: 90
    01-13 17:21:34.081 D/Ilias: Offset: 85 *** dx: 0 *** dy: -23 *** totalScrolled: 85
    01-13 17:21:34.101 D/Ilias: Offset: 62 *** dx: 0 *** dy: 11 *** totalScrolled: 62
    01-13 17:21:34.121 D/Ilias: Offset: 73 *** dx: 0 *** dy: 31 *** totalScrolled: 73
    01-13 17:21:34.131 D/Ilias: Offset: 104 *** dx: 0 *** dy: -3 *** totalScrolled: 104
    01-13 17:21:34.151 D/Ilias: Offset: 101 *** dx: 0 *** dy: -23 *** totalScrolled: 101
    01-13 17:21:34.171 D/Ilias: Offset: 78 *** dx: 0 *** dy: 37 *** totalScrolled: 78
    01-13 17:21:34.181 D/Ilias: Offset: 115 *** dx: 0 *** dy: 34 *** totalScrolled: 115
    01-13 17:21:34.201 D/Ilias: Offset: 149 *** dx: 0 *** dy: 13 *** totalScrolled: 149
    01-13 17:21:34.261 D/Ilias: Offset: 162 *** dx: 0 *** dy: 33 *** totalScrolled: 162
    01-13 17:21:34.271 D/Ilias: Offset: 195 *** dx: 0 *** dy: 4 *** totalScrolled: 195
    01-13 17:21:34.281 D/Ilias: Offset: 199 *** dx: 0 *** dy: 5 *** totalScrolled: 199
    01-13 17:21:34.301 D/Ilias: Offset: 204 *** dx: 0 *** dy: 3 *** totalScrolled: 204
    01-13 17:21:34.321 D/Ilias: Offset: 207 *** dx: 0 *** dy: 2 *** totalScrolled: 207
    01-13 17:21:34.341 D/Ilias: Offset: 209 *** dx: 0 *** dy: 2 *** totalScrolled: 209
    01-13 17:21:34.351 D/Ilias: Offset: 211 *** dx: 0 *** dy: 1 *** totalScrolled: 211

我希望你能帮助我!

THX

1 个答案:

答案 0 :(得分:1)

好的,我发现了问题。

我的特殊问题是基于"亲属"循环查看的y轴坐标值。我解释一下:

我们的活动顶部有一个工具栏。 我们使用recyclerView的片段是"低于"工具栏。

所以在我们的例子中,工具栏的初始高度为224px。 每次打电话都是" onMoved"在我们的OnscrollListener中,我们减少了工具栏的高度,分别设置了滚动距离的平移。

那么,这里发生了什么? 随着每次迭代减少工具栏的高度,"高度" RecyclerView的数量增加了。因此,如果我们增加顶部的RecyclerView,那么500 px高度的RecyclerView(例如)的y坐标当然不是​​相同的y坐标。

    ________________   
1  | Toolbar        | 
2  |                | height: 200px
3  |________________|  
1  | RecyclerView   |  
2  |                |  
3  |                |  <---- y-coordinate of the recyclerView
4  |                |        at this point: 256 (for example)
5  |                |  
6  |                |  
7  |                |  
8  |                |
9  |                |
10 |________________|  


    ________________ 
1  | Toolbar        | 
2  |________________|  
1  | RecyclerView   |  
2  |                |  
3  |                |  
4  |                |  <---- y-coordinate of the recyclerView
5  |                |        at this point: 380 (for example)
6  |                |  
7  |                |  
8  |                |  
9  |                |
10 |                |
11 |________________|  

将来可能会对你有所帮助:)