When I implemented the pull-down refresh and pull-up loading of listview today, I encountered a problem, which means that I need to perform pull-down refresh and pull-up loading according to the sliding position in the listview. *
To be specific, the refresh operation will be performed only when my listview slides to the top when it slides to the top when it pulls to the bottom when it slides to the bottom when it pulls to the load operation.
So how to determine the sliding position of the listview? Actually, it's quite easy to solve. Let me tell you what I think:
The top judgment is based on whether the distance from the top of the listview is 0.
The bottom is judged based on whether the distance between the bottom of the last item in the listview and the top of the first item is the height of the entire listview.
The above two idea codes are implemented as follows:
private void setLiseners() { // listview sets sliding monitoring lsv_new_house.setOnScrollListener(new () { @Override public void onScrollStateChanged(AbsListView absListView, int i) { } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if(firstVisibleItem == 0){ View first_view = lsv_new_house.getChildAt(0); if(first_view != null && first_view.getTop() == 0){ // ("Scroll to the top"); bl_down = true;// You can pull down to refresh }else { // ("not to the top"); bl_down = false;// Not sliding to the top will not allow the pull-down to refresh } } if(firstVisibleItem + visibleItemCount == totalItemCount){ View last_view = lsv_new_house.getChildAt(lsv_new_house.getChildCount() - 1); // ("height = " + lsv_new_house.getBottom());// The distance between the bottom of the last item and the top of the first item of the listview// int h = lsv_new_house.getHeight(); // ("h = " + h); if(last_view != null && last_view.getBottom() == lsv_new_house.getHeight()){ // ("It has been scrolled to the bottom"); bl_up = true;// Can be loaded on }else { // ("Not to the bottom"); bl_up = false;// Not sliding to the bottom will not allow pull-up loading } } } }); }
The code is very simple, let's talk about it briefly. Get the view of the first item and the view of the last item, and make corresponding judgments.
Moreover, this judgment will be more accurate, because as long as the first item in the ListView appears on the upper side of the screen, even if only a part appears, the value of firstVisibleItem is still 0, and the onScroll() callback will occur; Similarly, even if only a part of the last item of ListView is displayed, the value of ((firstVisibleItem + visibleItemCount) == totalItemCount) is equal to totalItemCount.
However, after adding the distance from the top and the height of the entire listview, you can make accurate judgments.
The above method to determine whether the listview slides to the top and bottom in Android is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.