首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BottomSheetDialogFragment单击以打开另一个BottomSheetDialogFragment android

BottomSheetDialogFragment单击以打开另一个BottomSheetDialogFragment android
EN

Stack Overflow用户
提问于 2022-09-22 20:54:05
回答 1查看 57关注 0票数 0

我有一个ChatRoomActivity和一个NavHostFragment。默认片段是ConversatioFragment,它可以打开MessageChoicesFragment,也就是BottomSheetDialogFragment。我们有3个选择,1)复制消息,2)从3)删除消息读取。

如果我单击Read from (在其中我可以看到收件人已经读取了消息),我希望关闭MessageChoicesFragment并打开MessageReadDialogFragment BottomSheetFragment。

所以我想要的流是

ChatRoomActivity {

ConversationFragment --onClick-- MessageChoicesFragment --onClick-->

MessageReadDialogFragment

}

还有更多细节。navGraph的ChatRoomActivity如下所示

代码语言:javascript
复制
<?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时

代码语言:javascript
复制
override fun onRowLongClicked() {     
   findNavController().navigate(R.id.action_conversationFragment_to_messageChoicesFragment)
}

MessageChoicesFragment有3个按钮。当我按下"Read“按钮时,我想关闭MessageChoicesFragment并打开MessageReadDialogFragment

代码语言:javascript
复制
binding.readFromLayout.setOnClickListener {     
 findNavController().navigate(R.id.action_conversationFragment_to_messageReadDialogFragment)
 dialog?.dismiss()
}

但是,这不起作用,因为当前目的地无法找到从另一个目的地到“某处”的操作。

代码语言:javascript
复制
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

那我该怎么解决呢?

EN

回答 1

Stack Overflow用户

发布于 2022-09-23 08:39:36

或者您可以使用委托函数。在您的openReadFromBottomSHeet()函数中调用bottomSheetDialogFragment。它将在ConversatioFragment触发您的函数。然后,取消先前的底部页,打开一个选项ReadFrom的新底部。

代码语言:javascript
复制
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()
    }
}

差不多是这样的。对不起,我不能写所有的代码。这只是为了你的理解。各代表团对此很有帮助。我希望它能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73820576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档