Android AbsListView


This is an abstract class that meant to be used to implement virtualized lists(and grids,carousels,stacks etc) of items.

This class derives from AdapterView.

public abstract class AbsListView extends AdapterView<ListAdapter>

Several classes derive from this class:

View SubClass Type Description
GridView Direct Renders Items in a 2D scrolling grid.
ListView Direct Renders items in a scrollable vertical list.
ExpandableListView Indirect Renders items in a scrollable vertical two level list.

AbsListView has been available since Android API Level 1 and resides in the android.widget package.

Normally to render items, you won't use AbsListView, instead you'll use its children like ListView and GridView.

AbsListView is just the base class that provides the necessary abstraction to hold any type of list, be it grid, carousel or stack for these views.

AbsListView's subclasses like ListView and GridView rely on adapter for data binding. This is because AbsListView itself is an AdapterView.

Quick AbsListView Usage Examples

1. How to determine if an AbsListView can scroll vertically Up or Down.

We want to create a method that can help us determine if our abslistview widget can scroll vertically in both directions, up or down.

We'll start by creating a method returns us a boolean value. This method takes a View object as well as an integer. This view is what we will cast to our AbsListView.

The integer on the other hand will represent the direction of scroll as follows:

  • -1 - Scroll Up.
  • 1 - Scroll Down.
    public static boolean canScrollVertically(View targetView, int direction) {..}

Here's the full method:

    @SuppressWarnings("deprecation") 
    public static boolean canScrollVertically(View targetView, int direction) {
        if (Build.VERSION.SDK_INT < 14) {
            if (direction < 0) {
                if (targetView instanceof AbsListView) {
                    final AbsListView absListView = (AbsListView) targetView;
                    return absListView.getChildCount() > 0
                            && (absListView.getFirstVisiblePosition() > 0
                            || absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
                } else { 
                    return ViewCompat.canScrollVertically(targetView, direction) || targetView.getScrollY() > 0;
                } 
            }else{   
                if (targetView instanceof AbsListView) {
                    final AbsListView absListView = (AbsListView) targetView;
                    if (absListView.getCount() <= 0) {
                        return false; 
                    } else if (absListView.getLastVisiblePosition() < (absListView.getCount() - 1)) {
                        return true; 
                    } else { 
                        View lastView = absListView.getChildAt(absListView.getLastVisiblePosition() - absListView.getFirstVisiblePosition());
                        return !(lastView != null && lastView.getBottom() <= absListView.getMeasuredHeight());
                    } 
                } else if (targetView instanceof ScrollView) {
                    ScrollView scrollView = (ScrollView) targetView;
                    View child = scrollView.getChildAt(0);
                    return child != null && scrollView.getScrollY() < (child.getHeight() - scrollView.getMeasuredHeight());
                } else if (targetView instanceof WebView) {
                    WebView webView = (WebView) targetView;
                    return webView.getScrollY() < (webView.getContentHeight() * webView.getScale() - webView.getMeasuredHeight());
                } else { 
                    return ViewCompat.canScrollVertically(targetView, direction);
                } 
            } 
        }else{   
            returnViewCompat.canScrollVertically(targetView,direction);  
        } 
    } 

Then here's how we can expose that method:

    /** 
     * Determine if the view can scroll up 
     */ 
    public static boolean canScrollUp(View targetView) {
        return canScrollVertically(targetView, -1);
    } 

    /** 
     * Determine if the view can scroll down 
     */ 
    public static boolean canScrollDown(View targetView) {
        return canScrollVertically(targetView, 1);
    } 

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post