I have the following requirement:
- At first, data for page no: 2 is fetched from the server & the items are populated in a ListView.
Considering that both the prev page & next page are available in a scenario, the following code has been added:
if(prevPageNo > 0){
mListViewActual.setOnScrollListener(this);
}
if(nextPageNo > 0){
mListViewActual.setOnScrollListener(this);
}
What conditions should I put to detect scroll up & scroll down on the following methods:
- void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
- void onScrollStateChanged(AbsListView view, int scrollState)
After the action: scroll up & scroll down is detected , accordingly a service will be called with either the prev page no or next page no , to fetch the items to be populated in the Listview.
Any inputs will be helpful.
Gone through the following links but its not returning the correct scroll up / scroll down action:
To also detect scrolling with larger elements, I prefere an onTouch Listener:
Store the firstVisibleItem and on the next onScroll check if the new firstVisibleItem is smaller or greater than the previous one.
Example pseudocode (not tested):
My solution works perfectly giving the exact value for each scroll direction.
distanceFromFirstCellToTop
contains the exact distance from the first cell to the top of the parent View. I save this value inpreviousDistanceFromFirstCellToTop
and as I scroll I compare it with the new value. If it's lower then I scrolled up, else, I scrolled down.For Xamarin developers, the solution is the following:
Note: don't forget to run on UI thread
I've used this much simpler solution:
Here's what I would try first:
1) Create an interface (let's call it OnScrollTopOrBottomListener) with these methods:
void onScrollTop();
void onScrollBottom();
2) In your list's adapter, add a member instance, typed as the interface you created and supply a setter and getter.
3) In the getView() implementation of your adapter, check if the position parameter is either 0 or getCount() - 1. Also check that your OnScrollTopOrBottomListener instance is not null.
4) If the position is 0, call onScrollTopOrBottomListener.onScrollTop(). If position is getCount() - 1, call onScrollTopOrBottomListener.onScrollBottom().
5) In your OnScrollTopOrBottomListener implementation, call the appropriate methods to get the desired data.
Hope that helps in some way.
-Brandon