Implementation of Onscrolllistener to Detect the End of Scrolling in a Listview

Implementation of onScrollListener to detect the end of scrolling in a ListView

In the end I've reached a solution not so much elegant but that worked for me; having figured out that onScroll method is called for every step of the scrolling instead of just being called at the scroll end, and that onScrollStateChanged is actually being called only when scrolling is completed, I do something like this:

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
}

public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
}

private void isScrollCompleted() {
if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
/*** In this way I detect if there's been a scroll which has completed ***/
/*** do the work! ***/
}
}

Practically, everytime the ListView is being scrolled I save the data about the first visible item and the visible item count (onScroll method); when the state of scrolling changes (onScrollStateChanged) I save the state and I call another method which actually understand if there's been a scroll and if it's completed. In this way I also have the data about the visible items that I need.
Maybe not clean but works!

Regards

How to detect if a listview is scrolling up or down in android?

this is a simple implementation:

lv.setOnScrollListener(new OnScrollListener() {

private int mLastFirstVisibleItem;

@Override
public void onScrollStateChanged(AbsListView view, int scrollState){}

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

if(mLastFirstVisibleItem < firstVisibleItem){
// Scrolling down
}
if(mLastFirstVisibleItem > firstVisibleItem){
// scrolling up
}
mLastFirstVisibleItem = firstVisibleItem;
}
});

Android dynamic loading list view onScrollListener issues

To detect the item at the bottom of the screen you have to work with firstVisibleItem and visibleItemCount. I had created a demo example for the same using AsyncTask that will loading items in background and update the UI.

Total of firstVisibleItem and visibleItemCount will give you the number of items that are loaded and then you can implement the logic of loading more items.

int loadedItems = firstVisibleItem + visibleItemCount;

Find out if ListView is scrolled to the bottom?

Edited:

Since I have been investigating in this particular subject in one of my applications, I can write an extended answer for future readers of this question.

Implement an OnScrollListener, set your ListView's onScrollListener and then you should be able to handle things correctly.

For example:

private int preLast;
// Initialization stuff.
yourListView.setOnScrollListener(this);

// ... ... ...

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

switch(lw.getId())
{
case R.id.your_list_id:

// Make your calculation stuff here. You have all your
// needed info from the parameters of this function.

// Sample calculation to determine if the last
// item is fully visible.
final int lastItem = firstVisibleItem + visibleItemCount;

if(lastItem == totalItemCount)
{
if(preLast!=lastItem)
{
//to avoid multiple calls for last item
Log.d("Last", "Last");
preLast = lastItem;
}
}
}
}

how to find when the listview scrolling is finished?

You need to implement OnScrollListener which provides methods that can help you to detect the scrolling position of your ListView. Read this post.

How to detect scroll position of ListView in Flutter

I used NotificationListener that is a widget that listens for notifications bubbling up the tree. Then use ScrollEndNotification, which indicates that scrolling has stopped.

For scroll position I used _scrollController that type is ScrollController.

NotificationListener(
child: ListView(
controller: _scrollController,
children: ...
),
onNotification: (t) {
if (t is ScrollEndNotification) {
print(_scrollController.position.pixels);
}
//How many pixels scrolled from pervious frame
print(t.scrollDelta);

//List scroll position
print(t.metrics.pixels);
},
),


Related Topics



Leave a reply



Submit