Search code examples
androidkotlinandroid-recyclerviewretrofit2rx-java2

Load more recyclerview Items on click of "more" button with url


I am using retrofit to showing recyclerview items. At first only two of them is visible but on clicking of "show more" button, I am trying to load rest of the items with api.

my approach is to add more items with a url but not able to figure out if i am going in a correct way or not.

I am able to show starting 2 items and from back end , I am getting a url in "nextComments" tag. Passing this url as a parameter in api calling method. Trying to figure out the next approach

Api.getReplies(nextUrl.toString())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            { result ->
                if (!result.results.isNullOrEmpty()) {
                    nextUrl = result.next.toString()


                    //-------what should be here-----//?
                    replyAdapter.notifyDataSetChanged()
                }
            },
            { error ->
                println(error)
            })

The items are expected to add in recyclerview, when i click on show more


Solution

  • It really depends on how your adapter code looks like, but here is a generic example:

    //get the new items from backend
    var more_objects = await MyWebService.GetMoreObjects (indexKey);
    // add the items to the adapter
    _adapter.appendObjects(more_objects);
    _adapter.NotifyDataSetChanged ();
    

    appendObjects in the adapter:

        public void appendObjects (List<MyObj> obj)
        {
            foreach (var o in obj) {
                data.Add (o);
            }
        }
    

    appendObject in kotlin

    fun appendObjects(obj: List<MyObj>) {
        obj.forEach { 
            data.Add (it); 
        } 
    }