How to Detect Android Listview Scrolling Stopped

how to detect Android ListView Scrolling stopped?

Try using the setOnScrollListener and implement the onScrollStateChanged with scrollState == 0 ... do what you need to do...

setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}

public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
Log.i("a", "scrolling stopped...");
}
}
});

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;
}
});

Determine if the list view is about to stop scrolling?

You can set an OnScrollListener, and then store the value from absListView.getScrollY() each sample and compare it the previous sample to compute the velocity of the scroll. Once that drops below a threshold you define, you can take over scrolling.

Detect when ListView has reached the bottom - onScroll() or onScrollStateChanged()?

Here's some code for my suggested third approach, which I use in my own projects. I use the adapter's getView method to detect when the end of the list has been reached.

public View getView(int position, View convertView, ViewGroup parent) {
// handle recycling/creating/initializing view
if(reachedEndOfList(position)) loadMoreData();
return convertView;
}

private boolean reachedEndOfList(int position) {
// can check if close or exactly at the end
return position == getSize() - 1;
}

private void loadMoreData() {
// Perhaps set flag to indicate you're loading and check flag before proceeding with AsyncTask or whatever
}

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);
},
),

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.



Related Topics



Leave a reply



Submit