问题描述
我有onScrollStateChanged和onScroll事件监听一个ListView。我希望能够得到的ListView或办法让一些事件侦听器的启动滚动的finalX位置的滚动速度。我们的应用程序的目标SDK版本7。
I have a ListView with onScrollStateChanged and onScroll event listeners. I want to be able to get the scroll speed of the ListView or some way to get the finalX location of the initiated scroll in some Event listener. Our app targets SDK version 7.
我需要测量或获得的速度在ListView的滚动。
I need to measure or get the speed at which the ListView is scrolling.
推荐答案
在时间差分割第一个可见的项目不同的是没有一个很好的解决方案。 OnScroll监听临危onScroll事件时每隔一定时间,所以在大多数情况下,分工的结果将是0。
Division first visible items difference on time difference is not a good solution. OnScroll listener recieves onScroll event every fixed period of time, so in most cases the result of division will be "0".
所以,你可以尝试这样的:
So you can try something like this:
private OnScrollListener onScrollListener = new OnScrollListener() {
private int previousFirstVisibleItem = 0;
private long previousEventTime = 0;
private double speed = 0;
@Override
public void onScroll(HtcAbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (previousFirstVisibleItem != firstVisibleItem){
long currTime = System.currentTimeMillis();
long timeToScrollOneElement = currTime - previousEventTime;
speed = ((double)1/timeToScrollOneElement)*1000;
previousFirstVisibleItem = firstVisibleItem;
previousEventTime = currTime;
Log.d("DBG", "Speed: " +speed + " elements/second");
}
}
@Override
public void onScrollStateChanged(HtcAbsListView view, int scrollState) {
}
};
这篇关于如何得到一个ListView的滚动速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!