Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the VerticalScrollExtent in Android ScrollView?

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!

like image 462
Andrei Herford Avatar asked Jun 19 '14 08:06

Andrei Herford


3 Answers

According to the article from here:

enter image description here

I found this explanation correct.

like image 180
Ildar Zaripov Avatar answered Nov 06 '22 04:11

Ildar Zaripov


  • 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

like image 35
TheIT Avatar answered Nov 06 '22 06:11

TheIT


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 :)

like image 35
Dogcat Avatar answered Nov 06 '22 04:11

Dogcat