Whether Android RecyclerView slides to the bottom

  • RecyclerView whether to slide to the bottom:
  • NestedScrollView whether to slide band bottom/top:

RecyclerView whether to slide to the bottom:

RecyclerView. AddOnScrollListener (new RecyclerView. OnScrollListener () {/ / if the entry and then there were three, Private static final int THRESHOLD_LOAD_MORE = 3; private boolean hasLoadMore; @Override public void onScrollStateChanged(@NonNull final RecyclerView recyclerView, int newState) { if (newState == RecyclerView.SCROLL_STATE_DRAGGING) { hasLoadMore = false; } Log.i("onScrollStateChanged1", newState + ""); if (newState ! = RecyclerView.SCROLL_STATE_DRAGGING && ! HasLoadMore) {// The current distance from the bottom of the screen index int lastPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition(); Log.i("onScrollStateChanged2", lastPosition + ""); Int recyclerView.getAdapter().getitemCount () -lastPosition-1; Log.i("onScrollStateChanged3", recyclerView.getAdapter().getItemCount() + "\t" + offset); if (offset <= THRESHOLD_LOAD_MORE) { hasLoadMore = true; // load more // template.setdata (); }}}});Copy the code

The offset we get is a couple of subviews away from the bottom

NestedScrollView whether to slide band bottom/top:

NestedScrollView scroller = (NestedScrollView) findViewById(R.id.myScroll);
        scroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY > oldScrollY) {
                    LogUtil.i(TAG, "Finger press and slide");
                }
                if (scrollY < oldScrollY) {
                    LogUtil.i(TAG, "Finger lift slide");
                }

                if (scrollY == 0) {
                    LogUtil.i(TAG, "Now at the top");
                }

                if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    LogUtil.i(TAG, "It's sliding to the bottom."); }}});Copy the code

Note :nestedScrollview nested use recyclerView to judge the sliding to the bottom (nested sliding can not use recyclerView onSCrolled monitor)