What is VerticalScrollExtent in Android ScrollView?

from an answer to one of my other questions, I found a Google Demo subclass of ListView that allows you to reorder an item.

The demo works great, but it's hard for me to understand how it works: When an item is dragged above / below the edges of the ListView, the ListView starts scrolling up / down to open new items. The necessary calculation uses different parameters of the ScrollView subroutine:

public boolean handleMobileCellScroll(Rect r) { int offset = computeVerticalScrollOffset(); int height = getHeight(); int extent = computeVerticalScrollExtent(); int range = computeVerticalScrollRange(); int hoverViewTop = r.top; int hoverHeight = r.height(); if (hoverViewTop <= 0 && offset > 0) { smoothScrollBy(-mSmoothScrollAmountAtEdge, 0); return true; } if (hoverViewTop + hoverHeight >= height && (offset + extent) < range) { smoothScrollBy(mSmoothScrollAmountAtEdge, 0); return true; } return false; } 
  • height - the height of the ListView itself
  • offset is the scroll position = how many units / pixels have been scrolled up / down
  • range is the height of the full content.
  • extent - well, what is it?

ListView inherits computeVerticalScrollExtent() from the View , and the document says:

Calculate the vertical offset of the vertical scrollbar width within the horizontal range. This value is used to calculate the position of the thumb within the scroll bar.

If you look at code computeVerticalScrollExtent() , not one of the basements is implemented, but only directly View : It simply returns the height of the view.

This makes sense: if the ListView / ScrollView is 500 in height, the portion of the scroll content that is visible at a time is also 500. Is this a ScrollExtent value? Why is ScrollExtent required? Why not just use getHeight () directly?

I think something is missing and I am glad to any hint!

+7
android android-listview listview android-scrollview
source share
3 answers

It's a little late, but hope everything is in order.

1) The method is actually implemented by subclasses. For example, this is what it looks like for an AbsListView ( ListView superclass).
2) The height of the View may differ from its vertical scroll height - imagine a View with a strange top / bottom padding.

These two points make other issues largely irrelevant :)

+2
source share
  • compute * ScrollOffset . Defines the distance between the start of the scrollable area and the top of the current viewport in the scrollable area. For example, if your list contains 10 items and you scroll down so that the 3rd item is in the topmost visible item, then the offset is 3 (or 3 * itemHeight, see below).

  • compute * ScrollExtent . Determines the size of the current viewport in the scroll pane. So, for example, if your list contains 10 elements, and you can see 5 of these elements, then the size will be 5 (or 5 * itemHeight, see below).

  • compute * ScrollRange . Determines the size of the current scroll pane. For example, if your list contains 10 items, then the range is 10 (or 10 * itemHeight, see below).

Please note that all these methods can return values ​​in different units depending on their implementation, so for the above examples I use indexes, but in some cases these methods return pixel values ​​equivalent to the width or height of the elements.

In particular, the LinearLayoutManager RecyclerView returns indexes if the "smooth scroll bar" function is enabled, otherwise it will return pixel values. See ScrollbarHelper in the support library for more details.

Additional information: https://groups.google.com/forum/#!topic/android-developers/Hxe-bjtQVvk

+4
source share

This is sample code that can help you figure out how to get scrollBar top and bottom using computeVerticalScrollExtent:

 scrollbarTop = (float)this.computeVerticalScrollExtent()/this.computeVerticalScrollRange()*thisβ€Œβ€‹.computeVerticalScrollOffset(); scrollbarBottom = (float)this.computeVerticalScrollExtent()/this.computeVerticalScrollRange()*(thiβ€Œβ€‹s.computeVerticalScrollExtent()+this.computeVerticalScrollOffset()); 
+1
source share

All Articles