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
How to Encrypt File from Sd Card Using Aes in Android
Email Address Validation in Android on Edittext
How to Make Constraintlayout Work with Percentage Values
No Actionbar in Preferenceactivity After Upgrade to Support Library V21
Generate APK File from Aab File (Android App Bundle)
Panic: Broken Avd System Path. Check Your Android_Sdk_Root Value
How to Shrink Code - 65K Method Limit in Dex
Android - Get Children Inside a View
How to Get the Md5 Fingerprint from Java's Keytool, Not Only Sha-1
How to Downgrade My Sdk Version
Sending Text Messages Programmatically in Android
Enable/Disable Zoom in Android Webview
Get the Current Fragment Object
Using Android Vector Drawables on Pre Lollipop Crash