2

I am trying to parse JSONArray in RecyclerView using volley in android, i have 10 data per page, how can I call next page url when we scroll down recyclerview every time?

My JsonArray

"data": {
    "total": 146,
    "per_page": 10,
    "current_page": 1,
    "last_page": 15,
    "next_page_url": "http://www.xxxxxxxxxx.com/api/v1/auth/deal?page=2",
    "prev_page_url": null,
    "from": 1,
    "to": 10,
    "data": [
        {
            "id": "209",
            "ref": "101hd-209",
            "pid": "437",
            "name": "Office For Rent",
            "deal_mode": {
                "id": "128",
                "category_name": "Lease"
            },

My recyclerview load 1st page data at a time, now on scroll down how i call next page url to load next page data.

Dinesh
  • 71
  • 2
  • 10
  • Simplest way is in `onBindViewHolder` check `if((list.size()==(position-1)))` if true then make callback to activity or fragment and add next page to your list in adapter. – Bek Dec 04 '17 at 06:11
  • did you make callback with `next_page_url`? – Bek Dec 04 '17 at 07:37
  • don't forget to call `notifyDataSetChanged()` after adding next page to your adapter – Bek Dec 04 '17 at 07:39
  • thanks, its worked – Dinesh Dec 04 '17 at 07:45
  • you're welcome. – Bek Dec 04 '17 at 07:59
  • Hello Bek, it download all data at a time, not when we scroll down recyclerview. – Dinesh Dec 05 '17 at 09:32
  • Hi Dinesh there is an error. I made a mistake. position+1 must be instead. This is the correct answer. `onBindViewHolder check if((list.size()==(position+1)))` – Bek Dec 05 '17 at 09:39
  • it was my mistake, i took url in for loop. but i want my recyclerview fetch next page data when we scroll down. now how i call next `page url` to get next `page data`, – Dinesh Dec 05 '17 at 09:50
  • I uploaded it as answer. Try it. If you have any questions ask. – Bek Dec 05 '17 at 09:52

2 Answers2

1

Replace MainActivity with your activity

//this code will be inside adapter in onBindViewHolder
if ((position+1) == dataList.size()){
        Log.d(TAG, "equal");
        ((MainActivity) context).loadNextPage(next_page_url);
}

//this code will be in MainActivity
public void loadNextPage(String pageUrl){
    //Here you make second page request
    //And add second page to list
    //then notify your adapter
}
Bek
  • 4,513
  • 3
  • 14
  • 29
0

I create interface onbottom and check if the recyclerview is on bottom then load more data. means count will post into the API first it loaded 1st page data means count=1 then when will reach onbottom call function again with count++ to load second page data

Its a same structure which i faced and this helped me fine.

interface:

public interface OnBottomReachedListener {
    void onBottomReached(int position);

}

adapter:

OnBottomReachedListener onBottomReachedListener;
 public void setOnBottomReachedListener(OnBottomReachedListener onBottomReachedListener){

        this.onBottomReachedListener = onBottomReachedListener;
    }
 @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
       final PropertyInfo propertyInfo = propertyInfoList.get(position);

        if (position == propertyInfoList.size() - 1  ){

            onBottomReachedListener.onBottomReached(position);

        }
}

Activity:

adapter.setOnBottomReachedListener(new OnBottomReachedListener() {
            @Override
            public void onBottomReached(int position) {
                count++;
                String URl="http://www.xxxxxxxxxx.com/api/v1/auth/deal?

                page="+count;




            }
        });
Tara
  • 671
  • 4
  • 18
  • How can i call `next page url` when we reached bottom of `recyclerview`. that what i want. – Dinesh Dec 05 '17 at 11:42
  • Hello Waleed Asim, Its worked, but problem is its reset my `recyclerview` again, i mean when i reached at bottom it load next page data but i reached at top of `recyclerview` again. every time when i reached at bottom it load next page data but throw me at top of recyclerview again and again. – Dinesh Dec 06 '17 at 05:23
  • @Dinesh where you call notifyDataSetChanged()? – Tara Dec 06 '17 at 05:35
  • where and how i call notifyDataSetChanged()? – Dinesh Dec 06 '17 at 05:45
  • just call it outside the loop if you add data in loop youradapter.notifyDataSetChanged(); – Tara Dec 06 '17 at 05:47