Find Out If Listview Is Scrolled 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;
}
}
}
}

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.

How to detect when FMX List View is scrolled to the bottom?

Further investigation I found the following in

function TListViewBase.GetItemRect(const AItemIndex: Integer): TRectF;

If you go deeper in

function TListViewBase.GetItemRelRect(const Index: Integer; const LocRect: TRectF;
const SideSpace: Integer = 0): TRectF;

then you will realize that the last item's Top is

listview1.GetItemRect(listview1.ItemCount-1).top+listview1.ScrollViewPos-listview1.SideSpace-listview1.LocalRect.top

whatever was your items variable height. This represents the value of FHeightSums[Index] which is a list containing the sum of height till the item with Index Index

Now to the problem: You want the calculation to be perfect. you want a touch down.

And this is it

procedure TForm5.ListView1ScrollViewChange(Sender: TObject);
var
Tmp_top : single;
begin
Tmp_top := listview1.GetItemRect(listview1.ItemCount-1).top+listview1.ScrollViewPos-listview1.SideSpace-listview1.LocalRect.top;

if Tmp_top+listview1.GetItemRect(listview1.ItemCount-1).height-listview1.Height=listview1.ScrollViewPos-2*listview1.SideSpace then
showmessage('touch down');

end;

Edit: if you further simplify this formula you will end up in the other answer with few upgrades

 procedure TForm5.ListView1ScrollViewChange(Sender: TObject);
begin

if listview1.GetItemRect(listview1.ItemCount-1).bottom=listview1.Height-listview1.SideSpace then
showmessage('touch down');

end;

now this will cover any changes in padding, margins, sidespace, searchbox visibility and searchbox size changing.

How to know if the user has scrolled to the top or bottom of a listview/scrollview

I have found a solution by checking the offset of the first or the last item, when the offset of those items is 0 then we have reached the bottom/top of the listview.

    listview.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0) {
// check if we reached the top or bottom of the list
View v = listview.getChildAt(0);
int offset = (v == null) ? 0 : v.getTop();
if (offset == 0) {
// reached the top:
return;
}
} else if (totalItemCount - visibleItemCount == firstVisibleItem){
View v = listview.getChildAt(totalItemCount-1);
int offset = (v == null) ? 0 : v.getTop();
if (offset == 0) {
// reached the bottom:
return;
}
}
}
});

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


Related Topics



Leave a reply



Submit