Skip to content

Commit

Permalink
add set recycler view
Browse files Browse the repository at this point in the history
  • Loading branch information
meisam committed Oct 14, 2019
1 parent 62fe61c commit 9ef8e52
Showing 1 changed file with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,8 +17,12 @@
public abstract class PaginatedAdapter<ITEM, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
private List<ITEM> 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);
Expand All @@ -28,8 +37,9 @@ public void submitItems(Collection<? extends ITEM> 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();
}
Expand All @@ -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) {
Expand Down

0 comments on commit 9ef8e52

Please sign in to comment.