0

I have following api call function and I'd like to know how can I make pagination for my data?

Logic

  1. Receiving data from API (done)
  2. Printing data into recyclerview (done)
  3. Making data paginate (either by scroll or by page numbers doesn't make any difference for me)

Questions

  1. How to make my data paginate?
  2. Is it best if I return data paginated by back-end or make them paginate in my app?

Current behavior

Currently my data are fully returned by data (without pagination) so if I have 1 item or 1000 items they all will be available at the same time (code below)

Code

private fun getProjects() {
    val queue = Volley.newRequestQueue(context)
    val url = "https://example.com/api/projects"

    val stringReq : StringRequest =
        object : StringRequest(
            Method.GET, url,
            Response.Listener { response ->
                val projects = Gson().fromJson(response, Projects::class.java)

                defText.isVisible = false
                total_projects_text = projects.total
                (activity as MainActivity?)?.setActionBarTitle("Projects ($total_projects_text)")

                // showing recyclerview data
                projectsRecycler.isVisible = true
                projectsRecycler.layoutManager = LinearLayoutManager(
                    context,
                    LinearLayoutManager.VERTICAL,
                    false
                )
                projectsRecycler.adapter = ProjectAdapter(context, projects)
            },
            Response.ErrorListener { error ->
                Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show()
                defText.isVisible = true
            }
        ){
            override fun getHeaders(): Map<String, String> {
                val headers = HashMap<String, String>()
                headers["Content-Type"] = "application/json"
                return headers
            }
        }
    queue.add(stringReq)
}
mafortis
  • 6,750
  • 23
  • 130
  • 288
  • How will make them paginate in app will help that's defeats the whole of pagination . Its the bytes of data travel on HTTP we are optimizing by using pagination . Your API should serve data by page and you need add UI logic for it call the next page when u reached at end of List . So first Your API need to be paged later you can add pagination to RecyclerView Something like https://stackoverflow.com/questions/26543131/how-to-implement-endless-list-with-recyclerview. – ADM Apr 20 '21 at 13:38
  • @ADM so what you suggest is that I make my data paginated by server side right? and about `you need add UI logic for it call the next page when u reached at end of List` part can you give an example? (I've never done pagination before in android :D) – mafortis Apr 20 '21 at 13:40
  • your answers are a simple search away if you want to do paging [paging library for android](https://developer.android.com/topic/libraries/architecture/paging) – a_local_nobody Apr 20 '21 at 14:00

0 Answers0