content: package com.example.y.mvp.widget;
  
  
  import android.content.Context;
  import android.support.v7.widget.GridLayoutManager;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.support.v7.widget.StaggeredGridLayoutManager;
  import android.util.AttributeSet;
  
  /**
   * by 12406 on 2016/4/30.
   */
  public class LoadMoreRecyclerView extends RecyclerView {
  
  
      private LAYOUT_MANAGER_TYPE layoutManagerType;
      /**
       * 最后一个的位置
       */
      private int[] lastPositions;
      /**
       * 最后一个可见的item的位置
       */
      private int lastVisibleItemPosition;
      private LoadMoreListener loadingData;
  
      public LoadMoreRecyclerView(Context context) {
          super(context);
      }
  
  
      public LoadMoreRecyclerView(Context context, AttributeSet attrs) {
          super(context, attrs);
  
      }
  
  
      public LoadMoreRecyclerView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
  
      public void setLoadingData(LoadMoreListener loadingData) {
          this.loadingData = loadingData;
      }
  
      @Override
      public void onScrolled(int dx, int dy) {
          super.onScrolled(dx, dy);
  
          RecyclerView.LayoutManager layoutManager = getLayoutManager();
          if (layoutManagerType == null) {
              if (layoutManager instanceof GridLayoutManager) {
                  layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
              } else if (layoutManager instanceof LinearLayoutManager) {
                  layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
              } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                  layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
              } else {
                  throw new RuntimeException(
                          "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
              }
          }
          switch (layoutManagerType) {
              case LINEAR:
                  lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                  break;
              case GRID:
                  lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
                  break;
              case STAGGERED_GRID:
                  StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                  if (lastPositions == null) {
                      lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                  }
                  staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
                  lastVisibleItemPosition = findMax(lastPositions);
                  break;
          }
      }
  
      @Override
      public void onScrollStateChanged(int state) {
          super.onScrollStateChanged(state);
          RecyclerView.LayoutManager layoutManager = getLayoutManager();
          int visibleItemCount = layoutManager.getChildCount();
          int totalItemCount = layoutManager.getItemCount();
          if (loadingData != null && visibleItemCount > 0 && state == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition == totalItemCount - 1) {
              loadingData.onLoadMore();
          }
      }
  
      private int findMax(int[] lastPositions) {
          int max = lastPositions[0];
          for (int value : lastPositions) {
              if (value > max) {
                  max = value;
              }
          }
          return max;
      }
  
      private enum LAYOUT_MANAGER_TYPE {
          LINEAR,
          GRID,
          STAGGERED_GRID
      }
  
      public interface LoadMoreListener {
          void onLoadMore();
  
      }
  }
  
  