Fragments are heavier than views. Creating a fragment for just one RecyclerView item might add unnecessary overhead, especially if that item is recycled frequently.
While using a separate fragment can help isolate the Compose state with its own ViewModel, it also introduces complexity. Managing fragment lifecycles inside a RecyclerView is uncommon and can lead to subtle bugs or unexpected behavior if not handled carefully.
ComposeView in RecyclerView: If your goal is to use Compose for that particular item, consider embedding a ComposeView directly in your RecyclerView adapter. This allows you to manage Compose state with a ViewModel scoped to the hosting fragment or even the composable itself without the overhead of an additional fragment. Hybrid or Full Compose Layout: If you’re leaning heavily on Compose, you might benefit from using a LazyColumn instead of a RecyclerView. This provides a more consistent Compose architecture and easier state management. ViewModel Scoping: You can still isolate the state for that particular item by using a dedicated ViewModel (or a scoped ViewModel) with the viewModel() function inside your composable. This avoids the need to introduce a fragment solely for state isolation.
Using a fragment with ViewPager2 in a RecyclerView item isn’t inherently wrong, but it’s not the most efficient or architecturally clean solution. Consider using a ComposeView or transitioning to a fully Compose-based layout to simplify state management and reduce complexity.