Cannot change whether this adapter has stable IDs while the adapter has registered observers

后端 未结 4 1433
难免孤独
难免孤独 2020-12-17 09:06

I am using RecyclerView in my android project and its performance has been really awful. From the answers here, I tried adding adapter.setHasStableIds(true); to

4条回答
  •  孤街浪徒
    2020-12-17 09:22

    In my case, it's because the I put the setHasStableIds(true) inside the Fragment's onActivityCreated(). The Fragment is the main point of my application so it never gets popped from back stack.

    class LocalFragment: Fragment() {
        private val adapter = LocalAdapter()
     
        override fun onActivityCreated(savedInstanceState: Bundle?) {
            adapter.setHasStableIds(true)
        }
    }
    

    The problem here is that the onActivityCreated() is called every time the Fragment is navigated to. However, as the Fragment is still alive in the back stack, the private val adapter field is still the same object that had its setStableIds(true) called previously.

    class LocalFragment: Fragment() {
        private var adapter = LocalAdapter()
     
        override fun onActivityCreated(savedInstanceState: Bundle?) {
            adapter = LocalAdapter() // Re-assign the adapter
            adapter.setHasStableIds(true)
        }
    }
    

    Solution is to reassign the RecyclerView.Adapter object every time the fragment is switched to.

提交回复
热议问题