Detect Scroll Up & Scroll Down in Listview

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

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 check if scroll position is at top or bottom in ListView?

You can use a ListView.builder to create a scrolling list with unlimited items. Your itemBuilder will be called as needed when new cells are revealed.

If you want to be notified about scroll events so you can load more data off the network, you can pass a controller argument and use addListener to attach a listener to the ScrollController. The position of the ScrollController can be used to determine whether the scrolling is close to the bottom.

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


Related Topics



Leave a reply



Submit