2

I would like to achieve a "snap" effect on an Android ListView. Specifically, when the ListView stops scrolling I would like it to stop at certain positions so that the first visible item is completely shown. To do that, I need to be able to estimate the final position that the ListView will stop at when the user stops dragging or flinging.

In the case of dragging, when we receive the ACTION_UP or ACTION_CANCEL event, I can simply read off the current Y offset as the final position.

In the case of flinging, however, when we receive the ACTION_UP or ACTION_CANCEL event, I will need some extra information to determine the final position that the ListView will stop scrolling at because of the deceleration period.

iOS provides such information through the targetContentOffset parameter in willEndDragging. Is there any equivalent information available in Android?

This is related to another question I asked but got no response: In Android, how to achieve snap effect on a listview while respecting acceleration caused by fling?

dementrock
  • 917
  • 3
  • 9
  • 21
  • I am not sure what you're asking for. Would you like to calculate the number of rows a Listview can hold? Or, do you like to calculate the Listview height? – The Original Android Mar 13 '15 at 22:34
  • @TheOriginalAndroid I've updated the question. Basically, in the case of flinging, when we receive the ACTION_UP or ACTION_CANCEL event, I will need some extra information to determine the final position that the ListView will stop scrolling at because of the deceleration period. – dementrock Mar 13 '15 at 23:07
  • I reviewed your other post with the code. Just as a suggestion to attract more attention to this post, I think your title does not fit the issue and some people like me thought it is related to an iOS issue, which I know nothing about. – The Original Android Mar 13 '15 at 23:48
  • @TheOriginalAndroid Thanks for the tip! I edited the title but do let me know if you have better suggestions. – dementrock Mar 13 '15 at 23:59

1 Answers1

0

I would override the OnScroll event of your TestListView like:

@Override
public void onScroll(AbsListView lw, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) {

    // Determine when the last row item is fully visible.
    final int lastItem = firstVisibleItem + visibleItemCount;
    if(lastItem >= totalItemCount) {
       Log.d("onScroll", "Reached last item");
       }
    }
}

Notice this listener gives you the first visible position/item, and the number of row items in the Listview.

I would use this listener because sometimes getFirstVisiblePosition() is not accurate, especially on a virtual ListView like what we're using. Another advantage of onScroll override method over onTouchEvent is that onScroll covers less triggers than onTouchEvent. And it is fast in my experience with it. Keep us posted and I am interested on this issue. Regards, Tommy Kwee

The Original Android
  • 6,147
  • 3
  • 26
  • 31