Skip to content

Instantly share code, notes, and snippets.

@rmyhal
Created July 26, 2020 14:35
Show Gist options
  • Save rmyhal/54a456599ad919a0b154e30c3a2ea2e1 to your computer and use it in GitHub Desktop.
Save rmyhal/54a456599ad919a0b154e30c3a2ea2e1 to your computer and use it in GitHub Desktop.
class PagerAdapter(private val activity: FragmentActivity) : FragmentStateAdapter(activity) {
private val items: ArrayList<PagerItem> = arrayListOf()
override fun createFragment(position: Int): Fragment =
PagerFragment.newInstance(items[position])
override fun getItemCount() = items.size
override fun getItemId(position: Int): Long {
return items[position].id.toLong()
}
override fun containsItem(itemId: Long): Boolean {
return items.any { it.id.toLong() == itemId }
}
fun setItems(newItems: List<PagerItem>) {
items.clear()
items.addAll(newItems)
notifyDataSetChanged()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment