Android:在recyclerview中获取用户所在屏幕上的确切位置 [英] Android: get the exact position on the screen that the user was at in a recyclerview

查看:489
本文介绍了Android:在recyclerview中获取用户所在屏幕上的确切位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将参考以下问题,因为这个人一个月前问了同​​样的事情,但是没有人回答他:

I'm going to reference the following question, because this person asked the same thing a month ago but nobody has answered him yet:

如何滚动用户准确位置,介于两个视图之间。?

当用户在Recyclerview列表中的某个位置滚动并最小化应用程序时,然后从bg恢复它 - 或进入另一个活动/片段并返回到recyclerview - 我希望列表滚动到用户所在的完全相同的位置。当我说出确切的位置时,我的意思是它必须是完全相同的位置 - 如果我只是告诉应用程序滚动到位置X,那么最顶层的视图将位于顶部,但它不会是完全相同的列表状态就像用户之前一样。我需要列表看起来与用户离开并返回之前的样子相同。这是可能的,如果是的话,怎么样?

When the user scrolls somewhere inside a recyclerview list and minimizes the app, then restores it from the bg - or goes into another activity/fragment and returns back to the recyclerview - I want the list to scroll to the exact same location the user was at. When I say the exact location I mean it has to be the exact same location - if I just tell the app to scroll to position X, then the topmost view will be at the top, but it won't be the same exact list state as the user had before. I need the list to look identical to the way it looked before the user left and returned. Is this possible, and if so, how?

我目前的代码:

viewHolder.objectInTheList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                        if (recyclerViewInterface != null) {

                            FileAccessUtil.getInstance().setIntegerProperty("recyclerListPosition", position);
                            recyclerViewInterface.onRecyclerviewListEventOccured(position);

                        }
                    }

            });

然后当我返回活动时,我从fileaccessutil加载该参数并执行:

and then when I return to the activity I load that parameter from fileaccessutil and do:

 recyclerlist.scrollToPosition(positionFromFileAccessUtil);

此解决方案不是最理想的,因为正如我之前提到的,它不会将用户滚动到确切的位置与之前相同的屏幕位置。

This solution is suboptimal because, as I mentioned before, it doesn't scroll the user to the exact identical screen position that he was at before.

推荐答案

你看过 RecyclerView.ScrollBy(x,y)

ScrollBy(0,y)将以与向上移动手指时相同的方向滚动RecyclerView。它不会滚动到绝对位置,相反,它只会将像素添加到前一个滚动位置。要向相反方向滚动,只需使用 ScrollBy(0,-y)。请注意,x设置为零,因为您只想向上/向下滚动。

ScrollBy(0,y) will scroll the RecyclerView in the same direction as it would if you were to move your finger up. It won't scroll to an absolute position, instead, it will just add pixels to the previous scroll position. To scroll in the opposite direction, simply use ScrollBy(0,-y). Notice that x is set to zero because you only want to scroll up/down.

现在,为了获得完整的解决方案,您需要有一种方法来保存列表的当前滚动位置。我没有测试它,但我很确定 RecyclerView.getScrollY()将始终返回0.为了解决这个问题,你需要通过监听来追踪你的位置。滚动更改为RecyclerView。

Now, in order to achieve a complete solution, you will need to have a way to save the list's current scroll position. I did not test it, but I am pretty sure that RecyclerView.getScrollY() will always return 0. To overcome this, you will need to track the position yourelf by listening for scroll changes to the RecyclerView.

当您想要恢复滚动位置时,您可以使用保存的当前位置,因为RecyclerView最初的滚动值为0 。

When you want to restore the scroll position, you can use your saved current position since the RecyclerView will, initially, have a scroll value of 0.

这篇关于Android:在recyclerview中获取用户所在屏幕上的确切位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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