I have created a activity Webview
content placed in Recyclerview
.
I want to get the scroll Y position when I scroll webview.
I tried Webview.getScrollY()
, Webview.getY()
, RecyclerView.getScrollY()
, RecyclerView.getY()
,... but it do not work fine. I can't get current scroll Y.
Is there any suggest for get scroll Y of Webview
or RecyclerView
?
Asked
Active
Viewed 2.2k times
17

Sinh Phan
- 1,180
- 3
- 16
- 36
-
6Try `view.computeVerticalScrollOffset()` – oleynikd Aug 26 '16 at 17:38
2 Answers
24
Use a RecyclerView.OnScrollListener for the RecyclerView and a View.OnScrollChangeListener for the webview.
You'll have to keep track of the total scroll yourself, like this:
private int mTotalScrolled = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mTotalScrolled += dy;
}
});
...
}
private int getScrollForRecycler(){
return mTotalScrolled;
}

JoeyJubb
- 2,341
- 1
- 12
- 19
-
I've tried `RecyclerView.OnScrollListener`. But `onScrolled(RecyclerView recyclerView, int dx, int dy)` method in this just get `dy` not y position. – Sinh Phan Sep 08 '15 at 14:19
-
-
1Yes, it is right, but partially. When I use ```linearLayoutManager.scrollToPositionWithOffset(int, int)``` to manually change the position, I can't get the delta Y, namely dy is always 0 during this process. So I regard it not reliable enough and need a better way. – Xavier.S Jan 12 '16 at 06:54
-
Not reliable, this is not the answer. After scrolling a bit you easily get to a point where the manually accumulated y is out of sync. The RecyclerView is all the way up and I still get a non-zero y. I'll keep looking. – niqueco Mar 06 '16 at 17:22
-
37`recyclerView.computeVerticalScrollOffset()` would be a better answer than this. – Pkmmte Sep 16 '16 at 00:20
-
5Not really. `computeVerticalScrollOffset()` isn't the same as the number of pixels scrolled. RV uses it for showing a scrollbar and it's value is very imprecise. – Saket Oct 02 '20 at 17:56
-
Trying to access `mTotalScrolled ` inside `onScrolled` crashes the app. – Daniel Rotnemer Feb 05 '21 at 14:18
-
1this runs into issues when using notifyItemRemoved in the adapter since the removed item causes mTotalScrolled to change without calling onScroll – StarterPack Nov 05 '21 at 09:12
19
computeVerticalScrollOffset()
is more convenient at this situation as @Pkmmte mentioned.
mTotalScrolled = recyclerView.computeVerticalScrollOffset();

Stanley Ko
- 3,383
- 3
- 34
- 60
-
21it doesn't return scrollY of recycler, it returns _The vertical offset of the scrollbar's thumb_ – RadekJ Jun 01 '18 at 10:35