I've 3 Fragment - F1, F2, and F3. I've attached them in a PagerAdaper (viewPager2). The initialization of the PagerAdapter in PagerFragment (in onCreateView) is -
binding = PagerFargmentBinding.inflate(inflater, container, false)
adapter = PagerAdapter(this)
binding?.viewPager?.adapter = adapter
binding?.viewPager?.isSaveEnabled = false
Now, My PagerAdapter Implementation -->
class PagerAdapter(
fragment: PagerFragment,
) : FragmentStateAdapter(fragment) {
private var nextFragment: BaseFragment? = null
private var baseFragment: BaseFragment = F1()
override fun getItemCount() = 2
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> {
baseFragment
}
else -> {
nextFragment ?: DummyDialogFragment()
}
}
}
fun setNextFragment(page: Int) {
nextFragment = null
nextFragment = if (page == 1) {
F2()
} else if (page == 2) {
F3()
} else {
null
}
}
}
Now From my Pager Fragment I'm setting the next fragment -
fun navigateTo(page: Int) {
adapter.setNextFragment(page)
if (page == 0) {
binding?.viewPager?.currentItem = page
} else {
binding?.viewPager?.currentItem = 1
}
}
And From F1 fragment I'm calling this function with 1 and 2. This is working fine for the first time. If First time, If I load F2 fragment as a nextFragment then F3 fragment is not displaying. I've tried debugging and found out that the nextFragment value is updating but somehow the added fragment is not being removed from the pager Adapter.
Is there any way so that I can add / remove fragment dynamically?
I've taken implementation idea from this block - https://intensecoder.com/android-swipe-fragments-with-viewpager2-in-kotlin/
CodePudding user response:
Well after quite a bit of exploring over stackoverflow and then the help of one of my peer, I've figure out the solution. The solution is -
Actually the problem was, the createFragment() was not triggering after the first time. So I've to override two function of StatePagerAdapter - getItemViewType() and getItemId(). My Implementation now -
class PagerAdapter(
fragmentManager: FragmentManager,
lifecycle: Lifecycle
) : FragmentStateAdapter(fragmentManager, lifecycle) {
private var viewType: Int = 0
override fun getItemCount() = 2
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> {
F1()
}
1 -> {
F2()
}
else -> {
F3()
}
}
}
override fun getItemViewType(position: Int): Int {
return when (position) {
0 -> 0
else -> viewType
}
}
override fun getItemId(position: Int): Long {
return when (position) {
0 -> 0
else -> viewType.toLong()
}
}
fun setNextFragment(page: Int) {
this.viewType = page
}
}
and Calling this from pagger fragment -
fun navigateTo(page: Int) {
adapter.setNextFragment(page)
if (page == 0) {
binding?.viewPager?.currentItem = page
} else {
binding?.viewPager?.currentItem = 1
}
}
The setNextFragment() assigning the desired F2() or F3() fragment. And by the help of the override implementation on getItemId() and getItemViewType() the createFragment() is getting called each time and thus I can add / remove fragment dynamically.