Find if a ListView is scrolled "fully" to the bottom?

1.2k views Asked by At

What I Want

I want to detect if the ListView have been scrolled "fully" to the bottom. By the word "fully", I mean that the last element of the list should be fully visible.

What I Did

This is what I did.

list.setOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {

                    }

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

                        switch (view.getId()) {
                            case R.id.encList:

                                final int lastItem = firstVisibleItem + visibleItemCount;
                                if (lastItem == totalItemCount) {

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

                    }
                });

The Problem

My code works to some extent but doesn't provide the exact result I wish for. The log gets printed when the last element is visible, but I want it to be printed only when the last element is fully visible.

What modifications do I need in this code?

1

There are 1 answers

0
Vaizadoo On

Put this

if (list.getLastVisiblePosition() ==
 list.getAdapter().getCount() -1 &&
 list.getChildAt(list.getChildCount() - 1).getBottom() <= list.getHeight()){
//Scrolled end
}

in your onScroll