Уведомление о скроллинге в начало списка

  1. /**
  2.  * Created by naik on 22.04.16.
  3.  */
  4. public class DownTopScrollListener extends RecyclerView.OnScrollListener {
  5.  
  6.     public static final String TAG = makeLogTag(DownTopScrollListener.class);
  7.  
  8.     private static final int DEFAULT_THRESHOLD = 3;
  9.  
  10.     private final OnLoadMoreListener mMoreListener;
  11.     private final int mThreshold;
  12.  
  13.     public DownTopScrollListener(@NonNull OnLoadMoreListener listener) {
  14.         mMoreListener = listener;
  15.         mThreshold = DEFAULT_THRESHOLD;
  16.     }
  17.  
  18.     public DownTopScrollListener(@NonNull OnLoadMoreListener listener, int threshold) {
  19.         mMoreListener = listener;
  20.         mThreshold = threshold;
  21.     }
  22.  
  23.     public interface OnLoadMoreListener {
  24.         void onLoadMore();
  25.         boolean isLoading();
  26.     }
  27.  
  28.     @Override
  29.     public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  30.         super.onScrolled(recyclerView, dx, dy);
  31.         if (dy <= 0) {
  32.  
  33.             if (!mMoreListener.isLoading() && findFirstVisiblePosition(recyclerView) <= mThreshold) {
  34.  
  35.                 LOGD(TAG, "Detected loadmore in DownTopScrollListener");
  36.                 mMoreListener.onLoadMore();
  37.             }
  38.         }
  39.     }
  40.  
  41.     public static int findFirstVisiblePosition(RecyclerView recyclerView) {
  42.         RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  43.         if (layoutManager instanceof LinearLayoutManager)
  44.             return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
  45.  
  46.         else if (layoutManager instanceof GridLayoutManager)
  47.             return ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
  48.  
  49.         throw new IllegalArgumentException("RecyclerView use unsupported LayoutManager: " + layoutManager.getClass().getName());
  50.     }
  51.  
  52.     public static int findFirstCompletelyVisiblePosition(RecyclerView recyclerView) {
  53.         RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  54.         if (layoutManager instanceof LinearLayoutManager)
  55.             return ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
  56.  
  57.         else if (layoutManager instanceof GridLayoutManager)
  58.             return ((GridLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
  59.  
  60.         throw new IllegalArgumentException("RecyclerView use unsupported LayoutManager: " + layoutManager.getClass().getName());
  61.     }
  62.  
  63.     /**
  64.      * @return item offset (Y-axis) or -1 if ViewHolder in position not instantiated yet
  65.      */
  66.     public static int getItemOffset(RecyclerView recyclerView, int position) {
  67.         RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);
  68.         return viewHolder == null ? -1 : (int) viewHolder.itemView.getY();
  69.     }
  70. }
Достаточно установить этот слушатель (recyclerView.addOnScrollListener(...)) И передать в него реализацию интерфейса OnLoadMoreListener

Реклама

Мы в соцсетях

tw tg yt gt