Reputation: 6606
Learning how to use the OnScrollListener and I want to make a way I can detect everytime a new List item is visible when a user scrolls up or down in a listview, if the user scrolls down I want to make a counter increment by 1 for each new cell that enters the screen and if it scrolls up I want it to decrement, any help would go a long way, thanks.
counter = 0;
list.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// If list scroll up
counter++;
// If list scrolls down
counter--;
}
});
Upvotes: 3
Views: 5300
Reputation: 6606
I figured it out, the first visible item increments by one as you scroll down
counter = 0;
list.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Do nothing
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(firstVisibleItem > counter + 3 || firstVisibleItem < counter - 3){
counter = firstVisibleItem;
Toast.makeText(ListTestActivity.this,
"counter = " + counter,
Toast.LENGTH_LONG).show();
}
}
});
Upvotes: 3
Reputation: 8548
Use the firstVisibleItem
variable. When it changes, adjust your count appropriately.
Upvotes: 1