Android Open Source - FloatingActionButton Abs List View Scroll Detector






From Project

Back to project page FloatingActionButton.

License

The source code is released under:

MIT License

If you think the Android project FloatingActionButton listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.melnykov.fab;
/* w w  w  .  jav  a  2  s.  com*/
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AbsListView;

abstract class AbsListViewScrollDetector implements AbsListView.OnScrollListener {
    private int mLastScrollY;
    private int mPreviousFirstVisibleItem;
    private AbsListView mListView;
    private int mScrollThreshold;

    abstract void onScrollUp();

    abstract void onScrollDown();

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if(totalItemCount == 0) return;
        if (isSameRow(firstVisibleItem)) {
            int newScrollY = getTopItemScrollY();
            boolean isSignificantDelta = Math.abs(mLastScrollY - newScrollY) > mScrollThreshold;
            if (isSignificantDelta) {
                if (mLastScrollY > newScrollY) {
                    onScrollUp();
                } else {
                    onScrollDown();
                }
            }
            mLastScrollY = newScrollY;
        } else {
            if (firstVisibleItem > mPreviousFirstVisibleItem) {
                onScrollUp();
            } else {
                onScrollDown();
            }

            mLastScrollY = getTopItemScrollY();
            mPreviousFirstVisibleItem = firstVisibleItem;
        }
    }

    public void setScrollThreshold(int scrollThreshold) {
        mScrollThreshold = scrollThreshold;
    }

    public void setListView(@NonNull AbsListView listView) {
        mListView = listView;
    }

    private boolean isSameRow(int firstVisibleItem) {
        return firstVisibleItem == mPreviousFirstVisibleItem;
    }

    private int getTopItemScrollY() {
        if (mListView == null || mListView.getChildAt(0) == null) return 0;
        View topChild = mListView.getChildAt(0);
        return topChild.getTop();
    }
}




Java Source Code List

com.melnykov.fab.AbsListViewScrollDetector.java
com.melnykov.fab.FloatingActionButton.java
com.melnykov.fab.ObservableScrollView.java
com.melnykov.fab.RecyclerViewScrollDetector.java
com.melnykov.fab.ScrollDirectionListener.java
com.melnykov.fab.ScrollViewScrollDetector.java
com.melnykov.fab.sample.DividerItemDecoration.java
com.melnykov.fab.sample.ListViewAdapter.java
com.melnykov.fab.sample.MainActivity.java
com.melnykov.fab.sample.RecyclerViewAdapter.java