简体   繁体   English

onClick 与 dataBinding ViewModel 不起作用

[英]onClick with dataBinding an ViewModel doesn't work

Im working on a project and implementing the MVVM model with databinding and navigation .我正在处理一个项目并使用databindingnavigation实现 MVVM 模型。 My button is on a fragment that opens with a drawer menu item , the thing is when i click on the button it does nothing, the debugger doesn't go into the navigate method, I really don't know what I did wrong, can someone help? My button在一个用drawer menu item打开的fragment上,问题是当我点击按钮时它什么也不做,调试器没有进入导航方法,我真的不知道我做错了什么,可以有人帮忙吗?

MYACCOUNT CLASS : MYACCOUNT CLASS

class MyAccountFragment : BaseFragment() {

    private val vm: MyAccountViewModel by viewModel()


    override fun getViewModel(): BaseViewModel = vm

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = FragmentMyAccountBinding.inflate(inflater, container, false)
        context ?: return binding.root
        injectFeature()

        setToolbar(binding)
        subscribeUi(binding)

        return binding.root
    }

    /**
     * set toolbar
     * **/
    private fun setToolbar(binding: FragmentMyAccountBinding) {

        binding.appBarLayout.backClickListener = (activity as MainActivity).createOnBackClickListener()
    }

    /**
     * set ui
     * **/
    private fun subscribeUi(binding: FragmentMyAccountBinding) {
        binding.viewModel = vm
    }


}

MYACCVIEWMODEL MYACCVIEW模型


class MyAccountViewModel constructor() : BaseViewModel() {

    fun onAddRoomClick()
    {
        navigate(MyAccountFragmentDirections.actionMyAccountFragmentToAddRoomFragment())
    }
}

and in the xml i implemented the在 xml 中我实现了

android:onClick="@{() -> viewModel.onAddRoomClick()}"

Im using this pattern for all my Fragments and ViewModels , and i really dont know why it doesn't do anything, the vm initializes.我对我所有的FragmentsViewModels都使用这种模式,我真的不知道为什么它什么都不做,vm 初始化。 On the other drawermenu fragment I also have the onClick method and it navigates to the other fragment.在另一个drawermenu fragment我也有 onClick 方法,它导航到另一个片段。 So if anyone knows the solution that would be helpful, thank you in advance.因此,如果有人知道有帮助的解决方案,请提前致谢。

the answer was in the initialization of the viewModel.答案是在 viewModel 的初始化中。

the onClick method in xml is in a content_layout that is included in a fragment_layout and instead of binding.viewModel = vm I had to do binding.content_layout.viewModel = vm. xml 中的 onClick 方法在包含在 fragment_layout 中的 content_layout 中,而不是 binding.viewModel = vm 我必须做 binding.content_layout.viewModel = vm。

 private fun subscribeUi(binding: FragmentMyAccountBinding) {
        binding.contentMyAccount.viewModel = vm
    }

ViewModel is not supposed to handle any kind of navigation, it will just receive the event from the UI and pass it to the controller (which might be a fragment or activity) and the latter will handle the navigation... ViewModel 不应该处理任何类型的导航,它只会从 UI 接收事件并将其传递给控制器​​(可能是片段或活动),后者将处理导航......

So one way to solve your issue is to do the following:因此,解决您的问题的一种方法是执行以下操作:

ViewModel视图模型

   class MyAccountViewModel constructor() : BaseViewModel() {
private val _navigateToRoomFragEvent = MutableLiveData<Boolean>(false)
val navigateToRoomFragEvent:LiveData<Boolean>
    get()=_navigateToRoomFragEvent

fun onAddRoomClick()
{
    _navigateToRoomFragEvent.value=true
}
fun resetNavigation(){
    _navigateToRoomFragEvent.value=false
}

} }

Controller (Activity or Fragment)控制器(活动或片段)

inside **onCreate() (if it is an activity)** **onCreate() 内部(如果是活动)**

viewModel.navigateToRoomFragEvent.observe(this,Observer{navigate->
//boolean value
if(navigate){
navController.navigate(//action)
}
viewModel.resetNavigation() //don't forget to reset the event
})

onActivityCreated(if it is a fragment) onActivityCreated(如果是片段)

viewModel.navigateToRoomFragEvent.observe(viewLifeCycleOwner,Observer{navigate->
//boolean value
if(navigate){
navController.navigate(//action)
}
viewModel.resetNavigation() //don't forget to reset the event
})

Hope it helps,希望能帮助到你,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM