How to implement shared transition element from RecyclerView item to Fragment with Android Navigation Component?

前端 未结 5 1527
梦如初夏
梦如初夏 2020-12-30 06:38

I have a pretty straightforward case. I want to implement shared element transition between an item in recyclerView and fragment. I\'m using androi

5条回答
  •  有刺的猬
    2020-12-30 07:04

    Faced the same issue as many on SO with the return transition but for me the root cause of the problem was that Navigation currently only uses replace for fragment transactions and it caused my recycler in the start fragment to reload every time you hit back which was a problem by itself.

    So by solving the second (root) problem the return transition started to work without delayed animations. For those of you who are looking to keep the initial state when hitting back here is what I did :

    just adding a simple check in onCreateView as so

    private lateinit var binding: FragmentSearchResultsBinding
    
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return if (::binding.isInitialized) {
                binding.root
            } else {
                binding = DataBindingUtil.inflate(inflater, R.layout.fragment_search_results, container, false)
    
                with(binding) {
                    //doing some stuff here
                    root
                }
            }
    

    So triple win here: recycler is not redrawn, no refetching from server and also return transitions are working as expected.

提交回复
热议问题