我正在构建一个应用程序,我有一个聊天内容,我已将其移至片段中。以前可以用,但现在不行了。 我知道这是一个已知问题,但我仍然无法解决该问题。 当我调试时,我可以看到我从 firebase 获取消息,并且适配器上的列表不为空,但在
notifyDataSetChanged
之后,我看到 onBindViewHolder
或持有者从未被调用。
我尝试从 UiThread 和回调中,但我仍然没有看到消息
聊天片段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>
活动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>
在活动中我有:
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()
}
在我尝试过的片段代码中:
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)
}
问题可能是由 onCreateView 中的操作顺序引起的,而是执行此操作并返回 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
}