What is the VerticalScrollExtent in Android ScrollView?
Asked Answered
D

5

12

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

The demo works great, but I am having some trouble to understand how the it works: When an item is dragged above/below the bounds of the ListView, the ListView starts scrolling up/down to reveal new items. The necessary calculation uses different parameters of the underling ScrollView:

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;
}
  • heightis the height of the ListView itself
  • offsetis the scroll position = how many units/pixels have been scrolled up/down
  • rangeis the height of the complete content.
  • extent - well, what is this?

ListView inherits computeVerticalScrollExtent() from View and the docu says:

Compute the vertical offset of the vertical scrollbar's thumb within the horizontal range. This value is used to compute the position of the thumb within the scrollbar's track.

If one looks at the code computeVerticalScrollExtent() is not implemented by one of the sublasses but only directly by View: It simply returns the height of the view.

This makes sense: If the ListView/ScrollView has a height of 500, the part of the scroll content that is visible at a time is also 500. Is this the meaning of the ScrollExtent? Why is ScrollExtent necessary? Why not simply use getHeight() directly?

I think I am missing something and I am happy about any hint!

Daube answered 19/6, 2014 at 8:40 Comment(2)
Did you had more information on the "extend"? I'm kinda block on this tooRunnel
This is a sample code which might help you to understand as to how to get scrollBar top and bottom using computeVerticalScrollExtent: { scrollbarTop = (float)this.computeVerticalScrollExtent()/this.computeVerticalScrollRange()*this.computeVerticalScrollOffset(); scrollbarBottom = (float)this.computeVerticalScrollExtent()/this.computeVerticalScrollRange()*(this.computeVerticalScrollExtent()+this.computeVerticalScrollOffset()); }Mariandi
X
2

It's kinda late, but hope it's ok.

1) The method actually is implemented by the subclasses. For example, this is what it looks like for AbsListView (ListView's superclass).
2) View's height can be different from its vertical scroll's height - just imagine a View with weird top/bottom padding .

These two points pretty much make other questions irrelevant :)

Xenos answered 2/12, 2014 at 11:48 Comment(0)
C
14
  • compute*ScrollOffset - Defines the distance between the start of the scrollable area and the top of the current view window inside the scrollable area. So for example, if your list has 10 items and you've scrolled down so the 3rd item is at the top-most visible item, then the offset is 3 (or 3*itemHeight, see below).

  • compute*ScrollExtent - Defines the size of the current view window inside the scrollable area. So for example, if your list has 10 items and you can currently see 5 of those items, then the extent is 5 (or 5*itemHeight, see below).

  • compute*ScrollRange - Defines the size of the current scrollable area. So for example, if your list has 10 items then the range is 10 (or 10*itemHeight, see below).

Note that all these methods can return values in different units depending on their implementation, so for the examples above, I am using the indices, but in some cases these methods will return pixel values equivalent to the width or height of the items.

In particular, the LinearLayoutManager of the RecyclerView will return indices if the 'smooth scrollbar' feature is disabled, otherwise it will return pixel values. See ScrollbarHelper in the support library for more information.

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

Caracas answered 6/10, 2016 at 2:7 Comment(0)
X
2

It's kinda late, but hope it's ok.

1) The method actually is implemented by the subclasses. For example, this is what it looks like for AbsListView (ListView's superclass).
2) View's height can be different from its vertical scroll's height - just imagine a View with weird top/bottom padding .

These two points pretty much make other questions irrelevant :)

Xenos answered 2/12, 2014 at 11:48 Comment(0)
M
2

This is a sample code which might help you to understand as to 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()); 
Mariandi answered 30/5, 2016 at 7:28 Comment(0)
A
2

According to the article from here:

enter image description here

I found this explanation correct.

Angleaangler answered 27/1, 2021 at 21:19 Comment(0)
R
1

ListView with 30 items has scrollRange equals to 3000, that is due to scrollRange = numberOfItems * 100, thus scrollExtent = numberOfVisibleItems * 100 and scrollOffset = numberOfScrolledItems * 100. You can find evidance of these words in the source code of AbsListView

Rumple answered 6/4, 2019 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.