最近做一个项目,老板让做一个关于listview滑动时获得滑动距离的需求,我不会。
google了一下,发现老外真牛逼啊,代码如下
github地址:https://gist.github.com/mariotaku/1f6c31691fc2ed4ce514
public class ListScrollDistanceCalculator implements OnScrollListener {
private ScrollDistanceListener mScrollDistanceListener;
private boolean mListScrollStarted;
private int mFirstVisibleItem;
private int mFirstVisibleHeight;
private int mFirstVisibleTop, mFirstVisibleBottom;
private int mTotalScrollDistance;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (view.getCount() == 0) return;
switch (scrollState) {
case SCROLL_STATE_IDLE: {
mListScrollStarted = false;
break;
}
case SCROLL_STATE_TOUCH_SCROLL: {
final View firstChild = view.getChildAt(0);