package com.example.pagination
import android.os.Bundle
import android.widget.Button
import android.widget.ListView
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
class PaginationActivity : AppCompatActivity() {
private lateinit var listView: ListView
private lateinit var prevButton: Button
private lateinit var nextButton: Button
private val itemsPerPage = 10
private val totalPages = 5
private var currentPage = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pagination)
listView = findViewById(R.id.listView)
prevButton = findViewById(R.id.prevButton)
nextButton = findViewById(R.id.nextButton)
updateList()
prevButton.setOnClickListener {
if (currentPage > 1) {
currentPage--
updateList()
}
}
nextButton.setOnClickListener {
if (currentPage < totalPages) {
currentPage++
updateList()
}
}
}
private fun generateItemsForPage(page: Int): List<String> {
val start = (page - 1) * itemsPerPage + 1
val end = start + itemsPerPage - 1
return (start..end).map { "Item $it" }
}
private fun updateList() {
val items = generateItemsForPage(currentPage)
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, items)
listView.adapter = adapter
prevButton.isEnabled = currentPage > 1
nextButton.isEnabled = currentPage < totalPages
}
}
This solution creates a simple pagination screen in Android using Kotlin. It shows 10 items per page labeled "Item X".
We keep track of the current page with currentPage. The generateItemsForPage function creates the list of items for that page.
The updateList function updates the ListView with the current page's items and enables or disables the Prev and Next buttons depending on the page.
Click listeners on the buttons change the page number and refresh the list.
This approach is simple and easy to understand for beginners learning pagination basics.