Last active
November 8, 2019 12:13
-
-
Save satoshun/9d311cbece9140f663097e5edaed9490 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChildFragment : Fragment() { | |
private lateinit var binding: ChildFragBinding | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
binding = ChildFragBinding.inflate(inflater, container, false) | |
return binding.root | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
binding.viewPager.isUserInputEnabled = false | |
val adapter = ChildAdapter(this) | |
binding.viewPager.adapter = adapter | |
val childView = binding.viewPager[0] as RecyclerView | |
childView.setItemViewCacheSize(5) | |
} | |
} | |
class ChildAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) { | |
override fun getItemCount(): Int = 5 | |
override fun createFragment(position: Int): Fragment { | |
return when (position % 5) { | |
0 -> Child1Fragment() | |
1 -> Child2Fragment() | |
2 -> Child3Fragment() | |
3 -> Child4Fragment() | |
else -> Child5Fragment() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment