Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the scroll speed on a ListView?

Tags:

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.

like image 896
Joost Avatar asked Oct 25 '11 20:10

Joost


1 Answers

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) {     } }; 
like image 68
SSemashko Avatar answered Oct 01 '22 10:10

SSemashko