What is the recommended way to launch a DialogFragment from a ViewModel?

前端 未结 5 1519
感情败类
感情败类 2020-12-06 06:40

I have a list objects in a Recyclerview. When long-pressing an item I want to show a dialog with data from the item clicked.

The Recyclerview

5条回答
  •  执笔经年
    2020-12-06 07:36

    The hint from Bayoudh led me in the right direction, but I'm posting this to put the pieces together. Below is a cardview that is clickable. Since my ViewModel holds no reference to the activity we have to pass the view in question as a parameter.

    
    

    The android:onClick="@{(view) -> viewModel.onClick(view)}" statement takes the current view as a parameter so you can use it in the ViewModel to get context with view.getContext() as Bayoudh states.

提交回复
热议问题