Home how to do dynamic pagination in android recyclerview
Reply: 3

how to do dynamic pagination in android recyclerview

N_G
1#
N_G Published in 2018-02-14 10:06:16Z

I have PAGE_SIZE=10 and PAGE_NUM=1,per page it has to display 10 records while scrolling the page again it has to append with the next 10 records and has to update the PAGE_SIZE as well as every end of the 10th record it has to show the ProgressBar also dynamically.

Referred this link. http://blog.iamsuleiman.com/android-pagination-tutorial-getting-started-recyclerview/

I was trying to update the adapter but I am getting only the first 10 records,not the next upcoming records.

It would be very helpful if anyone guide me to address this issue.

Activity.java

     recyclerView.addOnScrollListener(new PaginationScrollListener((LinearLayoutManager) mLayoutManager) {
                    @Override
                    protected void loadMoreItems() {
                        isLoading=true;
                        PAGE_NUM = PAGE_NUM + 1;
                        getData();

                    }

                    @Override
                    public int getTotalPageCount() {
                        return PAGE_NUM;
                    }

                    @Override
                    public boolean isLastPage() {
                        return isLastPage;
                    }

                    @Override
                    public boolean isLoading() {
                        return isLoading;
                    }
                });

 public void getData(){
     client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {
                    @Override
                    public void onSuccess(RestRequest request, final RestResponse result) {
                        try {
                            responseJsonObject = result.asJSONObject();
                            runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                jsonArrayList = DataHelper.populateJSONObjectList(responseJsonObject);
                                                result= DataHelper.getList(jsonArrayList);
                                                Log.d("result list>>", result.size() + "");
                                                dataAdapter = new DataAdapter(MainActivity.this, result, client, jsonArrayList);
                                                recyclerView.setAdapter(dataAdapter);
                                               if(!isLoading){
                                                    /*if(result.size()==PAGE_SIZE)
                                                        progressBar.setVisibility(View.VISIBLE);
                                                    if((result.size()%PAGE_SIZE)!=1){
                                                        progressBar.setVisibility(View.GONE);}*/
                                                       dataAdapter.addData(result);

                                                }

                                                if (result.size() > PAGE_SIZE) {
                                                    isLoading = false;
                                                    dataAdapter .addLoadingFooter();
                                                } else {
                                                    isLastPage = true;
                                                }

        }

Adapter.java

public void addData(List<Item> list) {
        for (int i = 0; i < list.size(); i++) {
            this.list.add(list.get(i));
        }
        notifyDataSetChanged();
    }
 public void addLoadingFooter() {
        isLoadingAdded = true;
        add(new Item());
    }

public void removeLoadingFooter() {
        isLoadingAdded = false;

        int position = list.size() - 1;
        Item item = getItem(position);

        if (item != null) {
            list.remove(position);
            notifyItemRemoved(position);
        }
    }
stfbee
2#
stfbee Reply to 2018-02-14 10:34:03Z

Maybe you should add second type for loading footer to Adapter and add items via notifyItemAdded()?

Nouman Ch
3#
Nouman Ch Reply to 2018-02-14 10:40:45Z

The problem here is when you do pagination only first time you have to set the adapter

dataAdapter = new DataAdapter(MainActivity.this, result, client, jsonArrayList);
recyclerView.setAdapter(dataAdapter);

and then on every loadMore function

you have to add extra 10 items in the your jsonArrayList instead of setting new adapter each time.

If you set the adapter each time you will not have your previous items.

Hope you understand that on loadMore you have to call function which add 10 new items to your jsonArrayList using jsonArrayList.add(result) then you have to call notifyDataChanged on adapter object.

//use this in onCreate method

dataAdapter = new DataAdapter(MainActivity.this, result, client, jsonArrayList);                       
recyclerView.setAdapter(dataAdapter);





recyclerView.addOnScrollListener(new PaginationScrollListener((LinearLayoutManager) mLayoutManager) {
                    @Override
                    protected void loadMoreItems() {
                        isLoading=true;
                        PAGE_NUM = PAGE_NUM + 1;
                        getData();

                    }

                    @Override
                    public int getTotalPageCount() {
                        return PAGE_NUM;
                    }

                    @Override
                    public boolean isLastPage() {
                        return isLastPage;
                    }

                    @Override
                    public boolean isLoading() {
                        return isLoading;
                    }
                });

 public void getData(){
     client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {
                    @Override
                    public void onSuccess(RestRequest request, final RestResponse result) {
                        try {
                            responseJsonObject = result.asJSONObject();
                            runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                jsonArrayList = DataHelper.populateJSONObjectList(responseJsonObject);
                                                result= DataHelper.getList(jsonArrayList);
                                                Log.d("result list>>", result.size() + "");

                                               if(!isLoading){
                                                    /*if(result.size()==PAGE_SIZE)
                                                        progressBar.setVisibility(View.VISIBLE);
                                                    if((result.size()%PAGE_SIZE)!=1){
                                                        progressBar.setVisibility(View.GONE);}*/
                                                       dataAdapter.addData(result);

                                                }

                                                if (result.size() > PAGE_SIZE) {
                                                    isLoading = false;
                                                    dataAdapter .addLoadingFooter();
                                                } else {
                                                    isLastPage = true;
                                                }

        }
Dhaval Patel
4#
Dhaval Patel Reply to 2018-02-14 11:05:57Z

I tried to modify your code.

declarations :

int PAGE_SIZE=10; //Number of items per page
int PAge_NUM=1; // page num with websevices providing accordingly
boolean mIsLoading=false; // fetching more data
boolean isMoreDataAvailable=true; // more data still available

Recycler's Scroll Listener

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mIsLoading)
                    return;
                int visibleItemCount = mLayoutManager.getChildCount();
                int totalItemCount = mLayoutManager.getItemCount();
                int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
                if (pastVisibleItems + visibleItemCount >= totalItemCount) {
                    //Scrolled to End of list
                    if (dataAdapter != null && dataAdapter.getItemCount() < mTotalNoOfData && isMoreDataAvailable) {

                        getData();


      }
            }
        }
    });

Fetch data method; may be WS call

public void getData(){
     client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {
                    @Override
                    public void onSuccess(RestRequest request, final RestResponse result) {
                        try {
                            responseJsonObject = result.asJSONObject();
                            runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                jsonArrayList = DataHelper.populateJSONObjectList(responseJsonObject);
                                                result= DataHelper.getList(jsonArrayList);
                                                Log.d("result list>>", result.size() + "");
                        if(dataAdapter!=null && result.size()>0){
                        dataAdapter.addMoreDataToList(result); // just append more data to current list
                        }else{
                                                dataAdapter = new DataAdapter(MainActivity.this, result, client, jsonArrayList);
                                                recyclerView.setAdapter(dataAdapter);// setAdapter for first time only
                        }

                        if(result!=null){
                            if( result.size()<PAGE_SIZE)}
                            isMoreDataAvailable=false;
                            }
                        }


        }

Add this method to your adapter

public void addMoreDataToList(ArrayList result){
currentDataList.addAll(result);
notifyDataSetChanged();
}

I modification I might have removed some of your code. Hope this helps you..!!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305679 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO