1

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

3 Answers 3

1

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

        }
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

may I know how to get mTotalDialogsWithCurrentUser? @Dhaval
edited, its a int value suggesting total no of items available
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.