Android RecyclerView垂直滚动偏移错误

bhmjp9jg  于 2023-03-11  发布在  Android
关注(0)|答案(1)|浏览(133)

我有一个垂直的RecyclerView和它后面的一个图像。我想在用户滚动时改变图像的alpha。用户越向下滚动,alpha就越高,从而创建一个外观效果。
我的解决方案基于添加一个OnScrollListenercomputeVerticalScrollOffset

RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int scrollY = recyclerView.computeVerticalScrollOffset();
            float alpha = Math.max(0f, Math.min(scrollY / 1000f, 1f));
            image.setAlpha(alpha);
            Log.d("zzz", "scroll measuredHeight="+recyclerView.getMeasuredHeight() +", scrollY="+scrollY+", alpha="+alpha);
        }
    };

然而,我注意到当用户向下滚动时,scrollY并不一致地递增。在某个点上,即使用户进一步滚动,其值也会减小。我还注意到,如果我添加一个垂直的拇指,我可以看到它在滚动时跳回到很高的位置。这对我来说表明,可能是recyclerView的高度正在发生变化,这导致了这种情况。但尽管如此,我仍然想要一个解决方案来处理这个问题。下面是显示不同值的日志:

2023-03-09 08:59:29.325 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=440, alpha=0.44
2023-03-09 08:59:29.342 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=448, alpha=0.448
2023-03-09 08:59:29.359 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=454, alpha=0.454
2023-03-09 08:59:29.375 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=461, alpha=0.461
2023-03-09 08:59:29.392 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=467, alpha=0.467
2023-03-09 08:59:29.409 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=471, alpha=0.471
2023-03-09 08:59:29.425 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=477, alpha=0.477
2023-03-09 08:59:29.442 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=482, alpha=0.482
2023-03-09 08:59:29.461 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=487, alpha=0.487
2023-03-09 08:59:29.476 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=492, alpha=0.492
2023-03-09 08:59:29.494 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=496, alpha=0.496
2023-03-09 08:59:29.509 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=501, alpha=0.501
2023-03-09 08:59:29.526 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=506, alpha=0.506
2023-03-09 08:59:29.542 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=512, alpha=0.512
2023-03-09 08:59:29.561 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=520, alpha=0.52
2023-03-09 08:59:29.575 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=527, alpha=0.527
2023-03-09 08:59:29.592 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=534, alpha=0.534
2023-03-09 08:59:29.611 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=541, alpha=0.541
2023-03-09 08:59:29.626 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=546, alpha=0.546
2023-03-09 08:59:29.642 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=551, alpha=0.551
2023-03-09 08:59:29.659 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=556, alpha=0.556
2023-03-09 08:59:29.676 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=562, alpha=0.562
2023-03-09 08:59:29.692 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=568, alpha=0.568
2023-03-09 08:59:29.709 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=575, alpha=0.575
2023-03-09 08:59:29.726 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=583, alpha=0.583
2023-03-09 08:59:29.745 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=592, alpha=0.592
2023-03-09 08:59:29.829 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=607, alpha=0.607
2023-03-09 08:59:29.843 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=631, alpha=0.631
2023-03-09 08:59:29.859 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=636, alpha=0.636
2023-03-09 08:59:29.876 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=643, alpha=0.643
2023-03-09 08:59:29.893 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=649, alpha=0.649
2023-03-09 08:59:29.910 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=656, alpha=0.656
2023-03-09 08:59:29.926 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=663, alpha=0.663
2023-03-09 08:59:29.943 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=670, alpha=0.67
2023-03-09 08:59:29.960 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=676, alpha=0.676
2023-03-09 08:59:29.976 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=683, alpha=0.683
2023-03-09 08:59:29.993 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=689, alpha=0.689
2023-03-09 08:59:30.010 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=696, alpha=0.696
2023-03-09 08:59:30.027 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=703, alpha=0.703
2023-03-09 08:59:30.044 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=710, alpha=0.71
2023-03-09 08:59:30.060 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=718, alpha=0.718
2023-03-09 08:59:30.077 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=726, alpha=0.726
2023-03-09 08:59:30.093 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=734, alpha=0.734
2023-03-09 08:59:30.110 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=741, alpha=0.741
2023-03-09 08:59:30.127 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=749, alpha=0.749
2023-03-09 08:59:30.143 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=757, alpha=0.757
2023-03-09 08:59:30.162 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=764, alpha=0.764
2023-03-09 08:59:30.177 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=771, alpha=0.771
2023-03-09 08:59:30.193 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=779, alpha=0.779
2023-03-09 08:59:30.210 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=790, alpha=0.79
2023-03-09 08:59:30.227 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=800, alpha=0.8
2023-03-09 08:59:30.244 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=809, alpha=0.809
2023-03-09 08:59:30.260 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=818, alpha=0.818
2023-03-09 08:59:30.278 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=826, alpha=0.826
2023-03-09 08:59:30.294 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=835, alpha=0.835
2023-03-09 08:59:30.312 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=845, alpha=0.845
2023-03-09 08:59:30.327 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=856, alpha=0.856
2023-03-09 08:59:30.344 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=868, alpha=0.868
2023-03-09 08:59:30.360 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=879, alpha=0.879
2023-03-09 08:59:30.377 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=889, alpha=0.889
2023-03-09 08:59:30.394 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=900, alpha=0.9
2023-03-09 08:59:30.411 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=912, alpha=0.912
2023-03-09 08:59:30.428 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=924, alpha=0.924
2023-03-09 08:59:30.444 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=935, alpha=0.935
2023-03-09 08:59:30.460 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=947, alpha=0.947
2023-03-09 08:59:30.479 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=956, alpha=0.956
2023-03-09 08:59:30.495 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=967, alpha=0.967
2023-03-09 08:59:30.511 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=977, alpha=0.977
2023-03-09 08:59:30.528 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=988, alpha=0.988
2023-03-09 08:59:30.545 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1000, alpha=1.0
2023-03-09 08:59:30.562 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1010, alpha=1.0
2023-03-09 08:59:30.578 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1019, alpha=1.0
2023-03-09 08:59:30.595 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1028, alpha=1.0
2023-03-09 08:59:30.611 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1036, alpha=1.0
2023-03-09 08:59:30.628 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1045, alpha=1.0
2023-03-09 08:59:30.644 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1054, alpha=1.0
2023-03-09 08:59:30.661 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1062, alpha=1.0
…
2023-03-09 08:59:32.765 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1563, alpha=1.0
2023-03-09 08:59:32.782 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1571, alpha=1.0
2023-03-09 08:59:32.798 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1579, alpha=1.0
2023-03-09 08:59:32.815 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1587, alpha=1.0
2023-03-09 08:59:32.831 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1594, alpha=1.0
2023-03-09 08:59:32.870 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1601, alpha=1.0
2023-03-09 08:59:32.878 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1609, alpha=1.0
2023-03-09 08:59:32.886 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=958, alpha=0.958 <——— Here
2023-03-09 08:59:32.898 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=964, alpha=0.964
2023-03-09 08:59:32.915 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=971, alpha=0.971
2023-03-09 08:59:32.932 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=977, alpha=0.977
2023-03-09 08:59:32.948 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=983, alpha=0.983
2023-03-09 08:59:32.965 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=990, alpha=0.99
2023-03-09 08:59:32.982 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=995, alpha=0.995
2023-03-09 08:59:32.998 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1001, alpha=1.0
2023-03-09 08:59:33.015 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1006, alpha=1.0
2023-03-09 08:59:33.032 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1011, alpha=1.0
2023-03-09 08:59:33.049 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1017, alpha=1.0
2023-03-09 08:59:33.065 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1022, alpha=1.0
2023-03-09 08:59:33.082 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1026, alpha=1.0
2023-03-09 08:59:33.099 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1033, alpha=1.0
2023-03-09 08:59:33.115 30661-30661/com.superpowered.backtrackit D/zzz: scroll measuredHeight=1869, scrollY=1039, alpha=1.0

还有比使用computeVerticalScrollOffset更好的解决方案吗?谢谢!

kqlmhetl

kqlmhetl1#

您要传递多少个项目给适配器?如果数量足够大,您可以根据要显示的项目执行平滑的动画效果。
我猜您使用LinearLayoutManager,这样我们就可以获得最后一个完全可见的项目,然后计算alpha的百分比:

((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition()

相关问题