diff --git a/paginatedrecyclerview/src/main/java/com/softrunapps/paginatedrecyclerview/PaginatedAdapter.java b/paginatedrecyclerview/src/main/java/com/softrunapps/paginatedrecyclerview/PaginatedAdapter.java index 7397deb..dc8586d 100644 --- a/paginatedrecyclerview/src/main/java/com/softrunapps/paginatedrecyclerview/PaginatedAdapter.java +++ b/paginatedrecyclerview/src/main/java/com/softrunapps/paginatedrecyclerview/PaginatedAdapter.java @@ -1,8 +1,13 @@ package com.softrunapps.paginatedrecyclerview; +import android.app.Activity; +import android.content.Context; +import android.os.Debug; +import android.util.Log; import android.view.ViewGroup; import androidx.annotation.NonNull; +import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; @@ -12,8 +17,12 @@ public abstract class PaginatedAdapter extends RecyclerView.Adapter { private List mDataSet = new ArrayList<>(); private OnPaginationListener mListener; - private int mFirstPage = 1; - private int mItemCount = 10; + private int mStartPage = 1; + private int mCurrentPage = 1; + private int mPageSize = 10; + private RecyclerView mRecyclerView; + private boolean loadingNewItems = true; + @NonNull public abstract VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType); @@ -28,8 +37,9 @@ public void submitItems(Collection collection) { mDataSet.addAll(collection); notifyDataSetChanged(); if (mListener != null) { - if (collection.size() == mItemCount) { - mListener.onNextPage(++mFirstPage); + if (collection.size() == mPageSize) { + mListener.onNextPage(++mCurrentPage); + loadingNewItems = false; } else { mListener.onFinish(); } @@ -40,16 +50,70 @@ protected ITEM getItem(int position) { return mDataSet.get(position); } - public void setFirstPage(int mFirstPage) { - this.mFirstPage = mFirstPage; + public void setStartPage(int mFirstPage) { + this.mStartPage = mFirstPage; + this.mCurrentPage = mFirstPage; + } + + public int getStartPage() { + return mStartPage; + } + + public int getCurrentPage() { + return mCurrentPage; + } + + public RecyclerView getRecyclerView() { + return mRecyclerView; + } + + public void setRecyclerView(RecyclerView recyclerView) { + this.mRecyclerView = recyclerView; + initPaginating(); + setAdapter(); + } + + public void setDefaultRecyclerView(Activity activity, int recyclerViewId) { + RecyclerView recyclerView = activity.findViewById(recyclerViewId); + recyclerView.setLayoutManager(new LinearLayoutManager(activity)); + recyclerView.setHasFixedSize(true); + this.mRecyclerView = recyclerView; + initPaginating(); + setAdapter(); + } + + private void setAdapter() { + mRecyclerView.setAdapter(this); } - public int getFirstPage() { - return mFirstPage; + public void setItemCount(int pageSize) { + this.mPageSize = pageSize; } - public void setItemCount(int mItemCount) { - this.mItemCount = mItemCount; + private void initPaginating() { + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(RecyclerView recyclerView, int newState) { + super.onScrollStateChanged(recyclerView, newState); + } + + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + LinearLayoutManager layoutManager = LinearLayoutManager.class.cast(mRecyclerView.getLayoutManager()); + int totalItemCount = layoutManager.getItemCount(); + int lastVisible = layoutManager.findLastVisibleItemPosition(); + boolean endHasBeenReached = lastVisible + 2 >= totalItemCount; + if (totalItemCount > 0 && endHasBeenReached) { + if (mListener != null) { + if (!loadingNewItems) { + loadingNewItems = true; + mListener.onNextPage(mCurrentPage); + } + } + } + } + }); } public void setOnPaginationListener(OnPaginationListener onPaginationListener) {