Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current Y offset of a ScrollView

Tags:

android

ScrollView has a method for setting the x and y scroll offset, but no method for getting the current offset (all I'm really interested is the y offset, since ScrollView only supports vertical scrolling). I don't see any method that would work in the superclasses, and tried getTop() for the content view, which is always zero. Am I missing something?

like image 934
Rich Avatar asked Jul 24 '10 21:07

Rich


People also ask

How do I get ScrollView offset in React Native?

To get the current scroll position of ScrollView in React Native, we can set the onScroll prop of the ScrollView to a function that takes the scroll event object as an argument.

What is Contentoffset Uiscrollview?

The point at which the origin of the content view is offset from the origin of the scroll view.

How do I use scrollTo in ScrollView React Native?

To scroll to top of the ScrollView with React Native, we assign a ref to the ScrollView and call scrollTo on the ref's value. to create a ref with useRef and set that as the value of the ref prop of the ScrollView . Then we add a Button that calls ref. current.

What is the My ScrollView?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.


2 Answers

Call getScrollY() on the ScrollView

See here for the documentation: http://developer.android.com/reference/android/view/View.html#getScrollY%28%29

like image 103
Charles Harley Avatar answered Sep 24 '22 12:09

Charles Harley


Why don't you try something like this ?

targetScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {         @Override         public void onScrollChanged() {             int scrollX = targetScrollView.getScrollX();             Log.d(TAG, "scrollX: " + scrollX);         }     }); 
like image 27
Nikola Velimirovic Avatar answered Sep 23 '22 12:09

Nikola Velimirovic