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.
I need to measure or get the speed at which the ListView is scrolling.
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) { } };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With