Example usage for android.widget ScrollView computeVerticalScrollExtent

List of usage examples for android.widget ScrollView computeVerticalScrollExtent

Introduction

In this page you can find the example usage for android.widget ScrollView computeVerticalScrollExtent.

Prototype

protected int computeVerticalScrollExtent() 

Source Link

Document

Compute the vertical extent of the vertical scrollbar's thumb within the vertical range.

Usage

From source file:com.my.seams_carv.util.ScrollableViewUtil.java

/**
 * Return true if the given scrollable view is over scrolling in terms of
 * the given y momentum./* w w w .  j ava  2  s .com*/
 * <br/>
 * Usually it is called in the {@code dispatchTouchEvent}, {@code onTouchEvent},
 * or the {@code onInterceptTouchEvent}.
 *
 * @param view the scrollable view.
 * @param dy The y momentum.
 * @return true if the view is over scrolling in the vertical direction.
 */
public static boolean ifOverScrollingVertically(View view, float dy) {
    if (view == null || dy == 0)
        return false;

    // TODO: Test it.
    if (view instanceof ScrollView) {
        final ScrollView scrollView = (ScrollView) view;
        final View child = scrollView.getChildAt(0);
        if (scrollView.getHeight() >= child.getHeight())
            return true;

        if (dy > 0) {
            // Slide the thumb down to scroll up the list.
            return scrollView.getScrollY() == 0;
        } else {
            // Slide the thumb up to scroll down the list.
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();

            return child.getHeight() - scrollView.getScrollY() == scrollView.getHeight()
                    - (params.topMargin + params.bottomMargin);
        }
        //        } else if (view instanceof WebView) {
        //            WebView webView = (WebView) view;
        //
        //            // TODO: Complete it.
        //            if (dy > 0) {
        //                // Slide the thumb down to scroll up the list.
        //                return webView.computeHorizontalScrollOffset() == 0.f;
        //            } else {
        //                // Slide the thumb up to scroll down the list.
        //                return false;
        //            }
    } else if (view instanceof NestedScrollView) {
        final NestedScrollView scrollView = (NestedScrollView) view;
        final int scrollRange = scrollView.computeVerticalScrollRange()
                - scrollView.computeVerticalScrollExtent();

        if (scrollRange <= 0)
            return true;

        if (dy > 0) {
            // Slide the thumb down to scroll up the list.
            return scrollView.computeVerticalScrollOffset() == 0.f;
        } else {
            // Slide the thumb up to scroll down the list.
            return scrollView.computeVerticalScrollOffset() == scrollRange;
        }
    } else if (view instanceof ListView) {
        final ListView listView = (ListView) view;
        if (listView.getAdapter().getCount() == 0)
            return true;

        // TODO: Complete it.
        if (dy > 0) {
            // Slide the thumb down to scroll up the list.
            return listView.getScrollY() == 0;
        } else {
            // Slide the thumb up to scroll down the list.
            return false;
        }
    } else if (view instanceof RecyclerView) {
        final RecyclerView recyclerView = (RecyclerView) view;
        if (recyclerView.getAdapter().getItemCount() == 0)
            return true;

        final int scrollRange = recyclerView.computeVerticalScrollRange()
                - recyclerView.computeVerticalScrollExtent();

        if (dy > 0) {
            // Slide the thumb down to scroll up the list.
            return recyclerView.computeVerticalScrollOffset() == 0.f;
        } else {
            // Slide the thumb up to scroll down the list.
            return recyclerView.computeVerticalScrollOffset() == scrollRange;
        }
    }
    // TODO: Support more scrollable view.

    // Return true for the unsupported view because the dy is non-zero.
    return true;
}