SharedElementTransition Navigation with RecyclerView(PaginDataAdapter) isn't working

29 Views Asked by At

im trying to make a transition with Navigation and some RecyclerView items. Thast the code of the Adapter

class SchoolsPagingDataAdapter(val clickListener : (extras : FragmentNavigator.Extras) -> Unit) : PagingDataAdapter<School, SchoolsPagingDataAdapter.ViewHolder>(GenericDiffUtils()) {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder = ViewHolder(ItemColegioBinding.inflate(LayoutInflater.from(parent.context),parent,false))


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.setIsRecyclable(false)
        val item = getItem(position)
        item.notNull { holder.bind(it) }
    }

    inner class ViewHolder(val binding : ItemColegioBinding) : RecyclerView.ViewHolder(binding.root){
        fun bind(school : School){
            with(binding){
                tvNombreItemColegio.transitionName = school.id + "tv"
                ivColegio.transitionName = school.id + "iv"

                tvNombreItemColegio.text = school.label ?: ""
                btFavItemColegio.setOnCheckedChangeListener { _, isChecked ->
                    school.fav = isChecked
                }
                btFavItemColegio.isChecked = school.fav

                root.setOnClickListener{
                    clickListener(FragmentNavigatorExtras(
                        tvNombreItemColegio to "tv_colegio_detail",
                        ivColegio to "iv_colegio_detail"))
                }
            }
        }
    }
}

The problem I am having is that if I specify the unique transitionName in each item, nothing is animated, but if I assign it in the xml and comment the one in the code, the last visible item in the recycler is always animated

I have tried a lot of things and I can't get it to work, I leave the rest of the code here in case someone knows how to tell me what is happening

@AndroidEntryPoint
class SchoolsListFragment : BaseFragment<FrgColegiosListadoBinding>() {

    private val schoolViewModel: SchoolsViewModel by activityViewModels()
    override var paddingTop = 0
    private lateinit var adapterSchools : SchoolsPagingDataAdapter

    companion object{
        private const val TAG = "SchoolsListFragment"
    }

    // Queremos que el padre SchoolsListMapFragment lo controle
    override var addOnBackPressed = false

    override fun initializeView() {
        postponeEnterTransition()
        setAdapter()

        binding.rvListado.doOnPreDraw {
            startPostponedEnterTransition()
        }
    }

    private fun setAdapter() {
        if (!this::adapterSchools.isInitialized)
            adapterSchools = SchoolsPagingDataAdapter { clickSchool(it) }
        binding.rvListado.adapter = adapterSchools

    }

    private fun clickSchool(extras: FragmentNavigator.Extras) {
        navController.navigate(R.id.action_global_nav_graph_colegio,null,null,extras)
    }

    override fun initObservers() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                schoolViewModel.pagerSchools.collectLatest {padingData->
                    adapterSchools.submitData(padingData)
                }
            }
        }
    }
}
//onCreateView Detail Fragment
transition = TransitionInflater.from(requireContext()).inflateTransition(android.R.transition.explode)
sharedElementEnterTransition = transition
sharedElementReturnTransition = transition
0

There are 0 best solutions below