ViewPager release 1.0.0 version
For a normal RecyclerView, holder.itemView
gives me the view currently binding/ rendering.
However, FragmentStateAdapter's holder.itemView
only gives me a FrameLayout
My ViewPager adapater:
class MyFragmentStateAdapter(activity: FragmentActivity) :
FragmentStateAdapter(activity) {
var items = mutableListOf<String>()
override fun createFragment(position: Int): MyPageFragment {
val itemText = items[position]
return MyPageFragment.create(itemText)
}
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(
holder: FragmentViewHolder,
position: Int,
payloads: MutableList<Any>
) {
super.onBindViewHolder(holder, position, payloads)
val fragment = ??? as MyRefreshableInterface
fragment.refresh()
}
fun update(mutableListOf: MutableList<String>) {
this.items = mutableListOf
notifyDataSetChanged()
}
}
Context
I have a small and fix number of tabs displaying different information section of a user profile. Upon certain events, I need to refresh AUTOMATICALLY all the tabs ASAP. In other words, I need to refresh the currently-hidden tabs besides the current tab user is looking at.
ASAP = the next time user visits a currently-hidden tab. User goes there, first thing they see is a loading animation. That's good
Why not immediately?
Because hidden fragments could be detached/ destroyed. User is not looking at the right now anyway. Keeping all the fragments in the memory is also expensive
When user navigates to previous hidden tab say Addresses, onBindViewHolder
will be triggered (This is great compared to ViewPager 1) However, the gap is that I have no reference of the currently-selected fragment
Other findings
I've already try referencing fragmentActivity.supportFragmentManager.fragments
but it seems to have maximum of 4 fragments whereas I have 6 fragments/ pages, for the sake of testing