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

how to do dynamic pagination in android recyclerview

user4121
1#
user4121 Published in June 18, 2018, 8:45 pm

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);
        }
    }
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO