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

前端 未结 5 1531
感情败类
感情败类 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:39

    See the Variables section of the official documentation of the Data Binding Library. There you find a variable context you can use.

    A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.

    Basically you could just pass it to another variable like the viewModel to show the dialog from there.

    android:onClick="@{v -> viewModel.showDialog(context)}"
    

提交回复
热议问题