How to find out if the user scrolls at the top or bottom of the list viewview / scrollview

I am trying to find out when the user scrolls to the top or bottom of the list, and he can no longer scroll.

Now I use OnScrollListener to find out which list items are visible.

listview.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (totalItemCount - visibleItemCount == firstVisibleItem) { //last item visible } if (firstVisibleItem == 0) { //first item visible } } }); 
+6
source share
4 answers

I found a solution by checking the offset of the first or last item when the offset of these items is 0, then we 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; } } } }); 
+10
source

Try this way

  list.setOnScrollListener(new OnScrollListener() { @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } int mPosition=0; int mOffset=0; @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub int position = list.getFirstVisiblePosition(); View v = list.getChildAt(0); int offset = (v == null) ? 0 : v.getTop(); if (mPosition < position || (mPosition == position && mOffset < offset)){ // Scrolled up } else { // Scrolled down } } }); 
+2
source

getView method of the getView method when viewing a scroll list

check the position when your position is equal to the representation of the list of data sizes, so you are in the last element of the list

0
source
 @Override public void onScrollChanged() { // We take the last son in the scrollview View view = (View) scrollview.getChildAt(scrollview.getChildCount() - 1); int diff = (view.getBottom() - (scrollview.getHeight() + scrollview.getScrollY())); if (diff == 0 ) { // bottom of the scrollview } } 
0
source

Source: https://habr.com/ru/post/969699/


All Articles