我有一个ChatRoomActivity和一个NavHostFragment。默认片段是ConversatioFragment,它可以打开MessageChoicesFragment,也就是BottomSheetDialogFragment。我们有3个选择,1)复制消息,2)从3)删除消息读取。
如果我单击Read from (在其中我可以看到收件人已经读取了消息),我希望关闭MessageChoicesFragment并打开MessageReadDialogFragment BottomSheetFragment。
所以我想要的流是
ChatRoomActivity {
ConversationFragment --onClick-- MessageChoicesFragment --onClick-->
MessageReadDialogFragment
}
还有更多细节。navGraph的ChatRoomActivity如下所示
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/chat_room_nav_menu"
app:startDestination="@id/conversationFragment">
<fragment
android:id="@+id/conversationFragment"
android:name="com.example.ui.fragments.chatroom.ConversationFragment"
android:label="conversation_fragment"
tools:layout="@layout/conversation_fragment" >
<action
android:id="@+id/action_conversationFragment_to_messageChoicesFragment"
app:destination="@id/messageChoicesFragment" />
<action
android:id="@+id/action_conversationFragment_to_messageReadDialogFragment"
app:destination="@id/messageReadDialogFragment" />
</fragment>
<dialog
android:id="@+id/messageReadDialogFragment"
android:name="com.example.ui.dialogs.chatroom.MessageReadDialogFragment"
android:label="MessageReadDialogFragment"
tools:layout="@layout/message_read_panel"/>
<dialog
android:id="@+id/messageChoicesFragment"
android:name="com.example.ui.dialogs.chatroom.MessageChoicesFragment"
android:label="fragment_message_choices"
tools:layout="@layout/fragment_message_choices" />
</navigation>当消息上的用户longPress打开MessageChoicesFragment时
override fun onRowLongClicked() {
findNavController().navigate(R.id.action_conversationFragment_to_messageChoicesFragment)
}MessageChoicesFragment有3个按钮。当我按下"Read“按钮时,我想关闭MessageChoicesFragment并打开MessageReadDialogFragment
binding.readFromLayout.setOnClickListener {
findNavController().navigate(R.id.action_conversationFragment_to_messageReadDialogFragment)
dialog?.dismiss()
}但是,这不起作用,因为当前目的地无法找到从另一个目的地到“某处”的操作。
java.lang.IllegalArgumentException: Navigation action/destination com.example:id/action_conversationFragment_to_messageReadDialogFragment cannot be found from the current destination Destination(com.example:id/messageChoicesFragment) label=fragment_message_choices那我该怎么解决呢?
发布于 2022-09-23 08:39:36
或者您可以使用委托函数。在您的openReadFromBottomSHeet()函数中调用bottomSheetDialogFragment。它将在ConversatioFragment触发您的函数。然后,取消先前的底部页,打开一个选项ReadFrom的新底部。
ConversationFragment{
lateinit var messageChoicesDialogFragmentObj: MessageChoicesDialogFragment()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
messageChoicesDialogFragmentObj.onReadFromClick = {
messageChoicesDialogFragmentObj.dismiss()
openReadFrom()
}
}
fun openMessageChoicesFragment(){
// opens MessageChoicesDialogFragment
messageChoicesDialogFragmentObj = MessageChoicesDialogFragment()
messageChoicesDialogFragmentObj.show(childFragmentManager, "")
}
fun openReadFrom(){
// opens ReadFromBottomSheetDialogFragment
}
}
MessageChoicesDialogFragment{
private val onReadFromClick: () -> Unit,
fun onReadFromButtonClicked(){
onReadFromClick.invoke()
}
}差不多是这样的。对不起,我不能写所有的代码。这只是为了你的理解。各代表团对此很有帮助。我希望它能帮到你。
https://stackoverflow.com/questions/73820576
复制相似问题