Ask Your Question
3

How can Koin be used to achieve the use of the same fragment type in a viewpager but with different instances of viewmodel in each tab, with varying parameters?

asked 2022-01-27 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-10-09 04:00:00 +0000

ladyg gravatar image

To use Koin to achieve the use of the same fragment type in a viewpager but with different instances of viewmodel in each tab, with varying parameters, you can follow these steps:

  1. Define a ViewModelFactory class that takes parameters in its constructor.
class MyViewModelFactory(private val param1: String, private val param2: Int) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return modelClass.getConstructor(String::class.java, Int::class.java)
            .newInstance(param1, param2)
    }
}
  1. Define a Koin module for each ViewModel instance that you want to create.
val myModule = module {
    factory { (param1: String, param2: Int) -> MyViewModelFactory(param1, param2) }
    factory { (param1: String, param2: Int) -> MyViewModel(param1, param2) }
}
  1. In your Fragment, inject the ViewModelFactory and ViewModel instances for each tab.
class MyFragment : Fragment() {
    private val viewModelFactory by inject<(String, Int) -> ViewModelProvider.Factory>()
    private lateinit var viewModel: MyViewModel

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val position = arguments?.getInt("position") ?: 0
        val param1 = "param1 for tab $position"
        val param2 = position
        viewModel = ViewModelProvider(this, viewModelFactory(param1, param2)).get(MyViewModel::class.java)
    }
}
  1. Finally, when creating your viewPager adapter, pass in the arguments for each tab and use the same Fragment type.
class MyViewPagerAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager) {
    override fun getItem(position: Int): Fragment {
        val args = Bundle()
        args.putInt("position", position)
        val fragment = MyFragment()
        fragment.arguments = args
        return fragment
    }
    override fun getCount(): Int {
        return 3
    }
}

With this approach, you can reuse the same MyFragment instance in each tab of the ViewPager but with different instances of MyViewModel, each with varying parameters.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-01-27 11:00:00 +0000

Seen: 12 times

Last updated: Oct 09 '21