material-components / material-components/material-components-android
Container transform navigate up animation problem of RecyclerView in Fragment inside ViewPager2
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 17.4k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
Device OS: Android 12
Problem dependencies:
```
implementation("com.google.android.material:material:1.5.0")
implementation("androidx.core:core-ktx:1.7.0")
implementation("androidx.navigation:navigation-fragment-ktx:2.4.1")
implementation("androidx.navigation:navigation-ui-ktx:2.4.1")
implementation("androidx.viewpager:viewpager:1.1.0-alpha01")
```
FragmentC is the launching Fragment of FragmentD, which is the 2nd element of ViewPager2 in FragmentA, the launching animation worked well, but the navigateUp (click back button) has no animation.
All of these fragments are in one Activity
By following the instruction of [material.io](https://material.io/develop/android/theming/motion#container-transform), following is the simplified code
Activity:
```
// layout of activity
// mobile_navigation
...
// Class of activity
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
...
}
```
FragmentA:
```
override fun onCreate(savedInstanceState: Bundle?) {
...
exitTransition = Hold()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
...
val fragments = listOf(
FragmentB(),
FragmentC()
)
viewPager2.adapter = object : FragmentStateAdapter(this@FragmentA) {
override fun getItemCount() = fragments.size
override fun createFragment(position: Int) = fragments[position]
}
...
}
```
FragmentC:
```
// RecyclerViewAdapter
private inner class RecyclerViewAdapter(private val items: List, ... , private val onClick(View, Item) -> Unit): RecyclerView.Adapter() {
...
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
...
holder.root.transitionName = items[position].id
holder.root.setOnClickListener { view -> onClick(view, items[position]) }
...
}
...
}
private lateinit recyclerViewAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
exitTransition = Hold()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
...
recyclerView.adapter = RecyclerViewAdapter(items, ..., ) { rootView, item ->
navController.navigate(
FragmentADirections.actionNavAtoNavC(item),
FragmentNavigatorExtras(rootView to rootView.transitionName)
)
}
}
```
FragmentD:
```
private lateinit item: Item
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = MaterialContainerTransform()
item = requireArguments().getSerializable("item") as Item
}
...
private var _binding: FragmentDBinding? = null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = ... // inflate layout
binding.root.transitionName = item.id
return binding.root
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the navigation flow across FragmentA, FragmentC, and FragmentD using the activity layout and mobile_navigation graph shown in the issue. Inspect the exitTransition, sharedElementEnterTransition, FragmentNavigatorExtras, and navigateUp behavior, then verify that the container-transform animation works in both directions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- design, mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100