Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. class Paginator<T>(private val items: MutableList<T>, limit: Int = 4) {
  2. private val TOTAL_NUM_ITEMS = items.size
  3. private val ITEMS_PER_PAGE = limit
  4. var currentPage: Int = 0
  5.  
  6. fun getTotalPages() : Int{
  7. val remainingItems = TOTAL_NUM_ITEMS % ITEMS_PER_PAGE
  8. return if (remainingItems == 0) TOTAL_NUM_ITEMS / ITEMS_PER_PAGE
  9. else Math.round(((TOTAL_NUM_ITEMS / ITEMS_PER_PAGE) + 1).toDouble()).toInt()
  10. }
  11.  
  12. fun getCurrentItems(currentPage: Int): ArrayList<T>? {
  13. val startItem = if (currentPage == 0){0}
  14. else currentPage * ITEMS_PER_PAGE
  15.  
  16. val lastItem = if (currentPage == 0){4} else startItem + ITEMS_PER_PAGE
  17.  
  18. val currentItems: ArrayList<T> = ArrayList()
  19. return try {
  20. items.forEachIndexed { index, T ->
  21. if (index in startItem..(lastItem - 1))
  22. currentItems.add(T)
  23. }
  24. this.currentPage++
  25. currentItems
  26. } catch (e: Exception) {
  27. e.printStackTrace()
  28. null
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement