有没有办法检测滚动运动在listView中发生的距离。
我正在尝试实现一个自定义视图容器,它在垂直linearLayout或FrameLayout中取两个视图(仍然未定),并且简单地手动逐步移动/动画合并/重叠的两个子视图(如parallex效果)。
一旦两个子视图都在屏幕上,我想开始跟踪列表视图内滚动的移动,并逐渐将两个子视图对象移动到一起并停止在某一点将它们移动到一起。
我只能找到这个例子,它只是检测到它向上或向下滚动,但它没有具体说明已经发生的滚动/距离。
lv.setOnScrollListener(new OnScrollListener() {
private int mLastFirstVisibleItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(mLastFirstVisibleItem<firstVisibleItem)
{
Log.i("SCROLLING DOWN","TRUE");
}
if(mLastFirstVisibleItem>firstVisibleItem)
{
Log.i("SCROLLING UP","TRUE");
}
mLastFirstVisibleItem=firstVisibleItem;
}
});
没有像这样的接口回调作为示例:
public void onScrolling(int movementY){ //movementY value represents how much u have scrolled from the point of touching the listview and applying the scroll motion}
提前致谢
答案 0 :(得分:2)
见this one。它完全符合您的要求。
/**
* Created by mariotaku on 14/10/22.
*/
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);
mFirstVisibleItem = view.getFirstVisiblePosition();
mFirstVisibleTop = firstChild.getTop();
mFirstVisibleBottom = firstChild.getBottom();
mFirstVisibleHeight = firstChild.getHeight();
mListScrollStarted = true;
mTotalScrollDistance = 0;
break;
}
}
}
public int getTotalScrollDistance() {
return mTotalScrollDistance;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (totalItemCount == 0 || !mListScrollStarted) return;
final View firstChild = view.getChildAt(0);
final int firstVisibleTop = firstChild.getTop(), firstVisibleBottom = firstChild.getBottom();
final int firstVisibleHeight = firstChild.getHeight();
final int delta;
if (firstVisibleItem > mFirstVisibleItem) {
mFirstVisibleTop += mFirstVisibleHeight;
delta = firstVisibleTop - mFirstVisibleTop;
} else if (firstVisibleItem < mFirstVisibleItem) {
mFirstVisibleBottom -= mFirstVisibleHeight;
delta = firstVisibleBottom - mFirstVisibleBottom;
} else {
delta = firstVisibleBottom - mFirstVisibleBottom;
}
mTotalScrollDistance += delta;
if (mScrollDistanceListener != null) {
mScrollDistanceListener.onScrollDistanceChanged(delta, mTotalScrollDistance);
}
mFirstVisibleTop = firstVisibleTop;
mFirstVisibleBottom = firstVisibleBottom;
mFirstVisibleHeight = firstVisibleHeight;
mFirstVisibleItem = firstVisibleItem;
}
public void setScrollDistanceListener(ScrollDistanceListener listener) {
mScrollDistanceListener = listener;
}
public static interface ScrollDistanceListener {
void onScrollDistanceChanged(int delta, int total);
}
}