Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Difference between View.getLeft() vs View.getScrollX()

what is the difference between View.getLeft() vs View.getScrollX() ? Please don't copy and paste definition from the documentation, because I am about to do it for you below

getScrollX()

Return the scrolled left position of this view.

getLeft()

Left position of this view relative to its parent

I think those 2 values should be the same, but a sample program of mine, if i do View.scrollBy(20, 0) i see that getScrollX() will return 20 and the view is actually moved to the right, but getLeft() remains to be zero

i am confused, because if visually the view is being scrolled to the right by 20px, its left position should be updated as well, but it is still 0

obviously they can't be the same, otherwise there is no need to have 2 different methods that return the same result

please help

like image 904
XyzNullPointer Avatar asked Apr 05 '13 02:04

XyzNullPointer


1 Answers

getLeft() returns the views location relative to its parent. How it has scrolled does not impact this at all. Scrolling impacts the contents of the view, not its location.

a quote from the android documentation regarding getLeft():

For instance, when getLeft() returns 20, that means the view is located 20 pixels to 
the right of the left edge of its direct parent. 

getScrollX(), on the other hand, lets you know how the content in the view has moved.

View.scrollBy(20,0) affects the content in the view (like subviews of the view) and doesn't actually move the view relative to the view's parent.

like image 65
HalR Avatar answered Oct 26 '22 23:10

HalR