Easy Android Pagination

December 01, 2017 , , 0 Comments

Hello Friends we seen the problem with Pagination in Android.

Here Some Shortcut trick to apply Pagination in android.

DEMO FOR BETTER LEARNING : FULLDEMO DOWNLOAD

public class DataAdapter extends RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;

private List studentList;

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



public DataAdapter(List students, RecyclerView recyclerView) {
studentList = students;

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;
}
}
});
}
}

@Override
public int getItemViewType(int position) {
return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}

@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.list_row, parent, false);

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

vh = new ProgressViewHolder(v);
}
return vh;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof StudentViewHolder) {

Student singleStudent= (Student) studentList.get(position);

((StudentViewHolder) holder).tvName.setText(singleStudent.getName());

((StudentViewHolder) holder).tvEmailId.setText(singleStudent.getEmailId());

((StudentViewHolder) holder).student= singleStudent;

} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}

public void setLoaded() {
loading = false;
}

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

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


//
public static class StudentViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;

public TextView tvEmailId;

public Student student;

public StudentViewHolder(View v) {
super(v);
tvName = (TextView) v.findViewById(R.id.tvName);

tvEmailId = (TextView) v.findViewById(R.id.tvEmailId);

v.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),
"OnClick :" + student.getName() + " \n "+student.getEmailId(),
Toast.LENGTH_SHORT).show();

}
});
}
}

public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;

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


How To Use:
       mRecyclerView.setLayoutManager(mLayoutManager);

// create an Object for Adapter
mAdapter = new DataAdapter(studentList, mRecyclerView);

mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
//add null , so the adapter will check view_type and show progress bar at bottom
studentList.add(null);
mAdapter.notifyItemInserted(studentList.size() - 1);

handler.postDelayed(new Runnable() {
@Override
public void run() {
// remove progress item
studentList.remove(studentList.size() - 1);
mAdapter.notifyItemRemoved(studentList.size());
//add items one by one
int start = studentList.size();
int end = start + 20;

for (int i = start + 1; i <= end; i++) {
studentList.add(new Student("Student " + i, "AndroidStudent" + i + "@gmail.com"));
mAdapter.notifyItemInserted(studentList.size());
}
mAdapter.setLoaded();
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
}, 2000);

}
});

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

0 comments:

Popular Posts