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
is using data binding for each item and I am able to display data from the selected item using Log when long-pressing.
When trying to show a dialog, however, you need to get to the Activity
, which is not recommended to use in the ViewModel
object.
So how can I show the dialog?
Thanks, Ove
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)}"
I think using a binding adapter for a recyclerview and put the adapter n ViewModel, then make the viewmodel is the model of fragment and passing adapter for the setAdapter method in xml itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With