PAGINATION IN RECYCLERVIEW
Hello, Here is a simple pagination tutorial in recyclerview.
Step 1 : - Create a recycler view and its adapter, In my case i have created a custom adapter in order to show user's profile image.
Step 2 : - Add onscrollchange listener on your recycler view and put your login in this method.
recyclerView.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);
if(dy > 0) //check for scroll down
{
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading)
{
if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
{
loading = false;
Log.e(TAG, "Last Item Wow !");
loadData();
}
}
}
}
});
}
Step 3 : - Call your api and on success or failure make the loading flag true so that it could work on second call.
pd.setMessage("loading next 10 questions.");
if(pd != null && !pd.isShowing())
pd.show();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = lastcount; i<lastcount+threshold; i++){
}
try {
new ApiManager().getQuestions(MainActivity.this, "10", new ApiListener() {
@Override
public void onCallSuccess(QuesitonBean quesitonBean) {
if(pd != null && pd.isShowing())
pd.dismiss();
Log.e(TAG, "Success"+quesitonBean.getItems().size());
for (int i = 0; i < quesitonBean.getItems().size(); i++) {
list_items.add(quesitonBean.getItems().get(i));
adapter.notifyDataSetChanged();
loading = true;
}
}
@Override
public void onError() {
if(pd != null && pd.isShowing())
pd.dismiss();
loading = true;
Log.e(TAG, "error");
}
});
showProgress(true);
Thread.sleep(2000);
showProgress(false);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
This is it, now you are done with pagination, run the app and feel delighted.
Here is a simple demo code for the above tutorial.
happy coding :)
0 Comment(s)