Android’s viewpager slides to determine whether the current stop page is the last page

Step 1: write several global variables to record:

   /**
     * Record if the sliding page is the last page
     */
    private boolean isHomeLastPage = false;
    private boolean isHomeDragPage = false;

The second step is to get the last element by implementing the listening onpageselected() in VP

@Override
	     public void onPageSelected(int position) {
	            super.onPageSelected(position);
	
	            Log.e("videoPositionposition", position + "");
	            //get the last page/position equals the last element
	            isHomeLastPage = position == datas.size() - 1;
	      }

The third step is to implement the listening onpagescrollstatechanged() in VP to determine whether the current sliding state is in,

   @Override
            public void onPageScrollStateChanged(int state) {
                // 0: do nothing 1: start sliding 2: end sliding Scrolling listener
                isHomeDragPage = state == ViewPager2.SCROLL_STATE_DRAGGING;
                //judge that the last one will not be loaded
            }

The fourth step is to implement onpagescrolled() 1.
1. Judge whether it is the last element
2. Whether it is in sliding state,
3. The offset of posionoffsetpixels is 0.
the work is done, and the judgment of VP sliding to the last page has been done

 @Override
 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

       //Determine the last element and slide state, and the offset is 0
       Log.e("vp2CCC", "vp2CCC" + isHomeLastPage + " " + isHomeDragPage + " " + positionOffsetPixels);
       if (isHomeLastPage && isHomeDragPage && positionOffsetPixels == 0) { //the current page is the last page and is dragging and the pixel offset is 0
           Toast.makeText(getActivity(), "The current page is the last page", Toast.LENGTH_SHORT).show();
       } else {
       }
   }

Read More: