Android Pagination on RecyclerView

June 29, 2016 , 1 Comments

Hello this is my best Try to understand,


== Pagination Loading Adapter ==
public class WireStoryLoadMoreAdapter extends RecyclerView.Adapter {

List listItem;
Context context;

private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;

// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;

// set for listview and Recyclerview binding and Scoll listiner
public WireStoryLoadMoreAdapter(List WireStory, RecyclerView recyclerView) {

this.listItem = WireStory;

if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {

final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager
if (!loading
&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if (onLoadMoreListener != null) {
loading = true;

//its used for Loded Data set.
public void setLoaded() {
loading = false;

//its for swipetorefresh at that time clear the data
public void setNewDataArray() {
listItem = new ArrayList<>();

public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;

//Use This method its Handle all of item to add more data
public void setMoreData(List moreItemList) {
listItem.remove(listItem.size() - 1);

//FOCUS : Any position item is null at that time progressbar will seen
public int getItemViewType(int position) {
return listItem.get(position) != null ? VIEW_ITEM : VIEW_PROG;

//set the View as per uses
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_wirestory, parent, false);

vh = new WireViewHolder(v);
} else {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_bottom_progressbar, parent, false);

vh = new ProgressViewHolder(v);
return vh;

//Bind Your View
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

if (holder instanceof WireViewHolder) {
((WireViewHolder) holder).wirestory_title.setText(listItem.get(position).getTitle());
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);


public int getItemCount() {
return listItem.size();

//set Your ItemViewHolder
class WireViewHolder extends RecyclerView.ViewHolder {
TextView wirestory_title;

public WireViewHolder(View itemView) {
wirestory_title = (TextView) itemView.findViewById(;

//set Your Progress ViewHolder
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;

public ProgressViewHolder(View v) {
progressBar = (ProgressBar) v.findViewById(;

//this is interface for loding
public interface OnLoadMoreListener {
void onLoadMore();

How To Used
Logic Behind it:
After added All Item one more row add which is progressbar.


storyList=> List Collection Object,
recyclerView => recyclerview Object
 mRecyclerView = (RecyclerView) findViewById(;
mLayoutManager = new LinearLayoutManager(this);
// use a linear layout manager

WireStoryLoadMoreAdapter mAdapter = new WireStoryLoadMoreAdapter(storyList, recyclerView);
recyclerView.setAdapter(mAdapter); //recyclerview Object

mAdapter.setOnLoadMoreListener(new WireStoryLoadMoreAdapter.OnLoadMoreListener() {
public void onLoadMore() {
//add null , so the adapter will check view_type and show progress bar at bottom
storyList.add(null); //this is Main its for Loader
mAdapter.notifyItemInserted(storyList.size() - 1); //added one more line



Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google


i dont know said...


Popular Posts