|
| 1 | +package com.smarttoolfactory.home.adapter |
| 2 | + |
| 3 | +import androidx.fragment.app.Fragment |
| 4 | +import androidx.fragment.app.FragmentManager |
| 5 | +import androidx.lifecycle.Lifecycle |
| 6 | +import com.smarttoolfactory.core.ui.adapter.NavigableFragmentStateAdapter |
| 7 | +import com.smarttoolfactory.core.ui.fragment.navhost.NavHostContainerFragment |
| 8 | +import com.smarttoolfactory.home.R |
| 9 | + |
| 10 | +/** |
| 11 | + * FragmentStateAdapter to contain ViewPager2 fragments inside another fragment. |
| 12 | + * |
| 13 | + * * 🔥 Create FragmentStateAdapter with viewLifeCycleOwner instead of Fragment to make sure |
| 14 | + * that it lives between [Fragment.onCreateView] and [Fragment.onDestroyView] while [View] is alive |
| 15 | + * |
| 16 | + * * https://stackoverflow.com/questions/61779776/leak-canary-detects-memory-leaks-for-tablayout-with-viewpager2 |
| 17 | + */ |
| 18 | +class HomeViewPager2FragmentStateAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : |
| 19 | + NavigableFragmentStateAdapter(fragmentManager, lifecycle) { |
| 20 | + |
| 21 | + override fun getItemCount(): Int = 3 |
| 22 | + |
| 23 | + override fun createFragment(position: Int): Fragment { |
| 24 | + |
| 25 | + return when (position) { |
| 26 | + |
| 27 | + // Post List with Rxjava3 |
| 28 | + 0 -> NavHostContainerFragment.createNavHostContainerFragment( |
| 29 | + R.layout.fragment_navhost_post_list, |
| 30 | + R.id.nested_nav_host_fragment_post_list |
| 31 | + ) |
| 32 | + |
| 33 | + // Post List with Flow |
| 34 | + 1 -> NavHostContainerFragment.createNavHostContainerFragment( |
| 35 | + R.layout.fragment_navhost_post_list, |
| 36 | + R.id.nested_nav_host_fragment_post_list |
| 37 | + ) |
| 38 | + |
| 39 | + // Post List with Status |
| 40 | + else -> NavHostContainerFragment.createNavHostContainerFragment( |
| 41 | + R.layout.fragment_navhost_post_list, |
| 42 | + R.id.nested_nav_host_fragment_post_list |
| 43 | + ) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments