Java Code Examples for android.widget.AbsListView#smoothScrollBy()

The following examples show how to use android.widget.AbsListView#smoothScrollBy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeaderScrollHelper.java    From RichWebList with Apache License 2.0 6 votes vote down vote up
/**
 * 将特定的view按照初始条件滚动
 *
 * @param velocityY 初始滚动速度
 * @param distance  需要滚动的距离
 * @param duration  允许滚动的时间
 */
@SuppressLint("NewApi")
public void smoothScrollBy(int velocityY, int distance, int duration) {
    View scrollableView = getScrollableView();
    if (scrollableView instanceof AbsListView) {
        AbsListView absListView = (AbsListView) scrollableView;
        if (sysVersion >= 21) {
            absListView.fling(velocityY);
        } else {
            absListView.smoothScrollBy(distance, duration);
        }
    } else if (scrollableView instanceof ScrollView) {
        ((ScrollView) scrollableView).fling(velocityY);
    } else if (scrollableView instanceof RecyclerView) {
        ((RecyclerView) scrollableView).fling(0, velocityY);
    } else if (scrollableView instanceof WebView) {
        ((WebView) scrollableView).flingScroll(0, velocityY);
    }
}
 
Example 2
Source File: ScrollableHelper.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
public void smoothScrollBy(int velocityY, int distance, int duration) {
    View scrollableView = getScrollableView();
    if (scrollableView instanceof AbsListView) {
        AbsListView absListView = (AbsListView) scrollableView;
        if (Build.VERSION.SDK_INT >= 21) {
            absListView.fling(velocityY);
        } else {
            absListView.smoothScrollBy(distance, duration);
        }
    } else if (scrollableView instanceof ScrollView) {
        ((ScrollView) scrollableView).fling(velocityY);
    }  else if (scrollableView instanceof NestedScrollView) {
        ((NestedScrollView) scrollableView).fling(velocityY);
    } else if (scrollableView instanceof RecyclerView) {
        ((RecyclerView) scrollableView).fling(0, velocityY);
    } else if (scrollableView instanceof WebView) {
        ((WebView) scrollableView).flingScroll(0, velocityY);
    }
}
 
Example 3
Source File: ScrollableHelper.java    From ScrollableLayout with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
public void smoothScrollBy(int velocityY, int distance, int duration) {
    View scrollableView = getScrollableView();
    if (scrollableView instanceof AbsListView) {
        AbsListView absListView = (AbsListView) scrollableView;
        if (sysVersion >= 21) {
            absListView.fling(velocityY);
        } else {
            absListView.smoothScrollBy(distance, duration);
        }
    } else if (scrollableView instanceof ScrollView) {
        ((ScrollView) scrollableView).fling(velocityY);
    } else if (scrollableView instanceof RecyclerView) {
        ((RecyclerView) scrollableView).fling(0, velocityY);
    } else if (scrollableView instanceof WebView) {
        ((WebView)scrollableView).flingScroll(0,velocityY);
    }
}
 
Example 4
Source File: ScrollableHelper.java    From ScrollableLayout with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
public void smoothScrollBy(int velocityY, int distance, int duration) {
    View scrollableView = getScrollableView();
    if (scrollableView instanceof AbsListView) {
        AbsListView absListView = (AbsListView) scrollableView;
        if (Build.VERSION.SDK_INT >= 21) {
            absListView.fling(velocityY);
        } else {
            absListView.smoothScrollBy(distance, duration);
        }
    } else if (scrollableView instanceof ScrollView) {
        ((ScrollView) scrollableView).fling(velocityY);
    } else if (scrollableView instanceof RecyclerView) {
        ((RecyclerView) scrollableView).fling(0, velocityY);
    } else if (scrollableView instanceof WebView) {
        ((WebView) scrollableView).flingScroll(0, velocityY);
    }
}
 
Example 5
Source File: ScrollCompat.java    From SmoothRefreshLayout with MIT License 4 votes vote down vote up
public static boolean scrollCompat(View view, float deltaY) {
    if (view != null) {
        if (view instanceof AbsListView) {
            final AbsListView absListView = (AbsListView) view;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                absListView.scrollListBy((int) deltaY);
            } else if (absListView instanceof ListView) {
                // {@link android.support.v4.widget.ListViewCompat#scrollListBy(ListView,
                // int)}
                final ListView listView = (ListView) absListView;
                final int firstPosition = listView.getFirstVisiblePosition();
                if (firstPosition == ListView.INVALID_POSITION) {
                    return false;
                }
                final View firstView = listView.getChildAt(0);
                if (firstView == null) {
                    return false;
                }
                final int newTop = (int) (firstView.getTop() - deltaY);
                listView.setSelectionFromTop(firstPosition, newTop);
            } else {
                absListView.smoothScrollBy((int) deltaY, 0);
            }
            return true;
        } else if (view instanceof WebView
                || view instanceof ScrollView
                || view instanceof NestedScrollView) {
            view.scrollBy(0, (int) deltaY);
            return true;
        } else if (ViewCatcherUtil.isRecyclerView(view)) {
            RecyclerView recyclerView = (RecyclerView) view;
            if (!(recyclerView.getOnFlingListener() instanceof PagerSnapHelper)) {
                // Fix the problem of adding new data to RecyclerView while in Fling state,
                // the new items will continue to Fling
                if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_SETTLING) {
                    recyclerView.stopScroll();
                }
                view.scrollBy(0, (int) deltaY);
            }
            return true;
        }
    }
    return false;
}