Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Paginator<T>(private val items: MutableList<T>, limit: Int = 4) {
- private val TOTAL_NUM_ITEMS = items.size
- private val ITEMS_PER_PAGE = limit
- var currentPage: Int = 0
- fun getTotalPages() : Int{
- val remainingItems = TOTAL_NUM_ITEMS % ITEMS_PER_PAGE
- return if (remainingItems == 0) TOTAL_NUM_ITEMS / ITEMS_PER_PAGE
- else Math.round(((TOTAL_NUM_ITEMS / ITEMS_PER_PAGE) + 1).toDouble()).toInt()
- }
- fun getCurrentItems(currentPage: Int): ArrayList<T>? {
- val startItem = if (currentPage == 0){0}
- else currentPage * ITEMS_PER_PAGE
- val lastItem = if (currentPage == 0){4} else startItem + ITEMS_PER_PAGE
- val currentItems: ArrayList<T> = ArrayList()
- return try {
- items.forEachIndexed { index, T ->
- if (index in startItem..(lastItem - 1))
- currentItems.add(T)
- }
- this.currentPage++
- currentItems
- } catch (e: Exception) {
- e.printStackTrace()
- null
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement