Introduction
Pagination helps load data in small parts instead of all at once. This makes apps faster and saves memory.
Pagination helps load data in small parts instead of all at once. This makes apps faster and saves memory.
fun loadPage(pageNumber: Int, pageSize: Int) {
// Fetch data for the given page
}loadPage(1, 20)
loadPage(2, 10)
This app shows a list of items 5 at a time. When you tap "Load More", it shows the next 5 items.
import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.* class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { PaginationExample() } } } @Composable fun PaginationExample() { var page by remember { mutableStateOf(1) } val pageSize = 5 val allItems = (1..50).map { "Item $it" } val items = allItems.drop((page - 1) * pageSize).take(pageSize) Column { LazyColumn { items(items) { item -> Text(text = item) } } Button(onClick = { if (page * pageSize < allItems.size) page++ }) { Text(text = "Load More") } } }
Always check if more pages exist before loading to avoid errors.
Use lazy loading lists like LazyColumn for better performance.
Keep UI responsive by loading pages asynchronously if fetching from a server.
Pagination splits data into small pages to improve app speed and memory use.
Use page number and page size to control which data to load.
Update the UI to show new pages when users request more data.
val page = 3
val size = 5
val start = (page - 1) * size
println("Loading items from index $start to ${start + size - 1}")val page = 1
val size = 10
val start = page * size
println("Loading items from index $start to ${start + size - 1}")Pass the test and this pattern moves unknown