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:
this is a simple implementation:
and if you need more precision, you can use this custom ListView class:
an example for use: (don't forget to add it as an Xml Tag in your layout.xml)
Here is a working modified version from some of the above-indicated solutions.
Add another class ListView:
And an interface:
And finally how to use:
In your XML layout:
This is my first topic, do not judge me harshly. =)
Those methods cannot be used to detect scrolling directions directly. There are many ways of getting the direction. A simple code(untested) for one such method is explained below :
The idea is to calculate the distanceToEnd. If distanceToEnd increases, the user is scrolling up and if it decreases, the user is scrolling down. That will also give you the exact distance to the end of the list.
If you are just trying to know whether the user is scrolling up or down you can override the onInterceptTouchEvent to detect the scrolling direction like below :
Simple way to detect scroll up/down on android listview
dont forget
Trick about detect scroll up or down in listview, you just call this function on onScroll function in OnScrollListener of ListView.
I have encountered problems using some example where the cell size of ListView is great. So I have found a solution to my problem which detects the slightest movement of your finger . I've simplified to the minimum possible and is as follows:
PD: I use the MARGIN to not detect the scroll until you meet that margin . This avoids problems when I show or hide views and avoid blinking of them.