Android Pagination on RecyclerView

June 29, 2016 , 1 Comments

Hello this is my best Try to understand,

FULLDEMO DOWNLOAD

== 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
.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager
.findLastVisibleItemPosition();
if (!loading
&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
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);
WireStoryLoadMoreAdapter.this.notifyItemRemoved(listItem.size());
listItem.addAll(moreItemList);
this.notifyItemInserted(moreItemList.size());
WireStoryLoadMoreAdapter.this.notifyDataSetChanged();
}

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

//set the View as per uses
@Override
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
@Override
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);
}

}

@Override
public int getItemCount() {
return listItem.size();
}

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

public WireViewHolder(View itemView) {
super(itemView);
wirestory_title = (TextView) itemView.findViewById(R.id.item_wirestory_title);
}
}

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

public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}

//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.

 

Here,
storyList=> List Collection Object,
recyclerView => recyclerview Object
 mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
// use a linear layout manager
mRecyclerView.setLayoutManager(mLayoutManager);

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

mAdapter.setOnLoadMoreListener(new WireStoryLoadMoreAdapter.OnLoadMoreListener() {
@Override
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
LoadMoreData();
}
});


 

Contributer

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

1 comments:

i dont know said...

Phetinic

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *