https://kotlinlang.org logo
#compose
Title
# compose
p

prudhvi reddy

03/08/2022, 6:24 AM
Hi. I am trying to implement a screen with multiple tabs (accompanist - pager) so i did something like this:
Copy code
@Composable
fun Screen() {
  val screenViewModel: ScreenViewModel = hiltViewModel()
  val pages by screenViewModel.pages.collectAsState()
  val pagerState = rememberPagerState(pageCount = pages.size)

  HorizontalPager(state = pagerState) { index ->
    Column(modifier = Modifier.fillMaxWidth()) {
      // this viewmodel should be created with data from the page object (e.g. id)
      val pagerViewModel: PagerViewModel = hiltViewModel() // actual: same viewmodel on every page -> expected: every page get´s its own viewmodel
      ...
    }
  }
}
Is it possible to get a ViewModel per page via hilt or should i use a complete different approach than ViewModels in this case?
b

bodo

03/31/2022, 7:05 PM
I came across the same question. did you find a solution for this?