Search code examples
androidfirebasekotlinandroid-recyclerviewnotifydatasetchanged

notifyDataSetChanged on fragment does not work


I'm building an app, I have a chat I moved into a fragment. it worked before and now it doesn't. I understand it's a known problem but still I couldn't solve the issue. When I'm debugging I can see that I'm getting the messages from firebase and the list at the adapter is not empty but after notifyDataSetChanged I see that onBindViewHolder or the holders are never being called. I tried from UiThread and from the callback but I still doesn't see the messages

chat fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.saloon.ChatFragment">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview_chat"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


</FrameLayout>

activity xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".activities.SaloonMobileScreenActivity">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:tabIndicatorColor="@color/colorSecondary"
        app:tabSelectedTextColor="@color/colorSecondary"
        app:tabTextAppearance="@style/TabLayoutTextFont"
        app:tabTextColor="@color/colorPrimaryLight">

        <!--        android:id="@+id/tabChat"-->
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chat" />

        <!--        android:id="@+id/tabUsers"-->
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/users" />

    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="3dp"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabBar"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintVertical_bias="1.0">

    </androidx.viewpager2.widget.ViewPager2>

</androidx.constraintlayout.widget.ConstraintLayout>

In the Activity I have:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySaloonMobileScreenBinding.inflate(layoutInflater)
        setContentView(binding!!.root)

        setTabs()
        
    }

    private fun setTabs() {
        pagerAdapter = FragmentPagerAdapter(this)
        pagerAdapter.addFragment(ChatFragment(saloonOwnerUid!!, saloonName!!), resources.getString(R.string.chat))
        pagerAdapter.addFragment(UsersFragment(), resources.getString(R.string.users))
        binding!!.viewPager.adapter = pagerAdapter
        binding!!.viewPager.currentItem = 0
        TabLayoutMediator(binding!!.tabBar, binding!!.viewPager) { tab, position ->
            tab.text = pagerAdapter.getTabTitle(position)
        }.attach()
    }

And in the fragment code I tried:

    private val onDataChangeCb : (ArrayList<MessageModel>) -> Unit = { msgList ->
        messages!!.clear()
        messages!!.addAll(msgList)
        (context as SaloonMobileScreenActivity).runOnUiThread {
            msgAdapter!!.updateAdapter(msgList)
        }
        msgAdapter!!.notifyDataSetChanged()
        binding!!.recyclerviewChat.smoothScrollToPosition(msgAdapter!!.itemCount)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        messages = ArrayList()
        binding = FragmentChatBinding.inflate(layoutInflater)
        BaseBillboActivity.getDbManager().addChatListener(onDataChangeCb, saloonOwnerUid, saloonName)
        msgAdapter = MessagesAdapter(requireActivity(), messages!!, senderUid)
        binding!!.recyclerviewChat.layoutManager = LinearLayoutManager(context)
        binding!!.recyclerviewChat.adapter = msgAdapter
        return inflater.inflate(R.layout.fragment_chat, container, false)
    }

Solution

  • Issue might be caused by the order of operations in the onCreateView do this instead and return binding.root

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            messages = ArrayList()
            binding = FragmentChatBinding.inflate(layoutInflater)
    
        // Set up the RecyclerView after inflating the Layout
          binding!!.recyclerviewChat.layoutManager = LinearLayoutManager(context)
          msgAdapter = MessagesAdapter(requireActivity(), messages!!, senderUid)
          binding!!.recyclerviewChat.adapter = msgAdapter
    
          BaseBillboActivity.getDbManager().addChatListener(onDataChangeCb, 
          saloonOwnerUid, saloonName)
           return binding.root
    }