Уведомление о скроллинге в начало списка
- /**
- * Created by naik on 22.04.16.
- */
- public class DownTopScrollListener extends RecyclerView.OnScrollListener {
- public static final String TAG = makeLogTag(DownTopScrollListener.class);
- private static final int DEFAULT_THRESHOLD = 3;
- private final OnLoadMoreListener mMoreListener;
- private final int mThreshold;
- public DownTopScrollListener(@NonNull OnLoadMoreListener listener) {
- mMoreListener = listener;
- mThreshold = DEFAULT_THRESHOLD;
- }
- public DownTopScrollListener(@NonNull OnLoadMoreListener listener, int threshold) {
- mMoreListener = listener;
- mThreshold = threshold;
- }
- public interface OnLoadMoreListener {
- void onLoadMore();
- boolean isLoading();
- }
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- if (dy <= 0) {
- if (!mMoreListener.isLoading() && findFirstVisiblePosition(recyclerView) <= mThreshold) {
- LOGD(TAG, "Detected loadmore in DownTopScrollListener");
- mMoreListener.onLoadMore();
- }
- }
- }
- public static int findFirstVisiblePosition(RecyclerView recyclerView) {
- RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
- if (layoutManager instanceof LinearLayoutManager)
- return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
- else if (layoutManager instanceof GridLayoutManager)
- return ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
- throw new IllegalArgumentException("RecyclerView use unsupported LayoutManager: " + layoutManager.getClass().getName());
- }
- public static int findFirstCompletelyVisiblePosition(RecyclerView recyclerView) {
- RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
- if (layoutManager instanceof LinearLayoutManager)
- return ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
- else if (layoutManager instanceof GridLayoutManager)
- return ((GridLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
- throw new IllegalArgumentException("RecyclerView use unsupported LayoutManager: " + layoutManager.getClass().getName());
- }
- /**
- * @return item offset (Y-axis) or -1 if ViewHolder in position not instantiated yet
- */
- public static int getItemOffset(RecyclerView recyclerView, int position) {
- RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);
- return viewHolder == null ? -1 : (int) viewHolder.itemView.getY();
- }
- }
Достаточно установить этот слушатель (recyclerView.addOnScrollListener(...)) И передать в него реализацию интерфейса OnLoadMoreListener