
Cursor-Based Pagination with Infinite Scroll in Compose
Cursor-Based Pagination with Infinite Scroll in Compose Cursor-based pagination is efficient for large datasets. Implement infinite scroll by loading more items as the user scrolls. Data Model data class PaginatedResponse < T >( val data : List < T >, val nextCursor : String ? = null , val hasMore : Boolean = false ) data class Post ( val id : String , val title : String , val content : String , val timestamp : Long ) Repository with Pagination class PostRepository ( private val api : PostApi ) { suspend fun getPosts ( cursor : String ? = null ): PaginatedResponse < Post > { return api . fetchPosts ( cursor = cursor ) } } ViewModel State Management class PostViewModel ( private val repository : PostRepository ) : ViewModel () { private val _posts = MutableStateFlow < List < Post >>( emptyList ()) val posts : StateFlow < List < Post >> = _posts . asStateFlow () private val _isLoading = MutableStateFlow ( false ) val isLoading : StateFlow < Boolean > = _isLoading . asStateFlow () private
Continue reading on Dev.to Tutorial
Opens in a new tab


