Home > Net >  Shared element return transition doesn't working with RecyclerVIew
Shared element return transition doesn't working with RecyclerVIew

Time:02-10

I found some similar questions, but they didn't help me. I have two fragments: in one RecyclerView, and in the second opens detailed information on the selected item. When you click on a list item, the animation appears, but when you click on the back button, it does not.

Fragment with RecyclerView:

class ListFragment: Fragment() {

/**/

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    
    viewLifecycleOwner.lifecycleScope.launchWhenResumed {
        viewModel.overviewUiState.collectLatest {
            when (it) {
                is OverviewViewModel.UiState.Ready -> {
                    overviewAdapter.submitList(it.listOfItems) // submit data to adapter
                    binding.progressBar.isVisible = false
                }
                is OverviewViewModel.UiState.Loading -> {
                    binding.progressBar.isVisible = true
                }
                else -> Unit
            }
        }
    }

    viewLifecycleOwner.lifecycleScope.launchWhenResumed {
        viewModel.events.collectLatest {
            when (it) {
                is OverviewViewModel.Event.SelectItem -> {
                    activityViewModel.selectItem(it.item)
                    activity?.supportFragmentManager?.beginTransaction()
                            ?.addSharedElement(it.imageView, getString(R.string.end_transition_tag)) // !!!
                            ?.replace(R.id.fragment_container, DetailFragment.newInstance())
                            ?.addToBackStack(DetailFragment::class.simpleName)
                            ?.commit()
                }
            }
        }
    }
}

/**/

ViewHolder:

class ItemViewHolder(private val binding: ListItemBinding) :
    RecyclerView.ViewHolder(binding.root) {

    fun bind(item: Item) {
        /**/

        ViewCompat.setTransitionName(image, item.id.toString())
        itemView.setOnClickListener {
            onItemClickListener?.let { click -> click(binding.image, item) }
        }
    }
}

Detail fragment:

class DetailFragment : Fragment() {

/**/

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    
    postponeEnterTransition()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        sharedElementEnterTransition = TransitionInflater.from(requireContext())
            .inflateTransition(android.R.transition.move)
    }
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    ViewCompat.setTransitionName(binding.image, getString(R.string.end_transition_tag))
    startPostponedEnterTransition()

    viewLifecycleOwner.lifecycleScope.launchWhenResumed {
        viewModel.events.collectLatest {
            when (it) {
                is DetailViewModel.Event.GoBack -> {
                    activity?.supportFragmentManager?.popBackStack()
                }
            }
        }
    }

    /**/
}

/**/

CodePudding user response:

  •  Tags:  
  • Related