'How to make `onClick()` in xml call two methods?
I want to make onClick() in my layout xml trigger two methods from two viewmodels.
Code:
<Button
android:onClick="@{() -> model.onButtonClick()}"
... />
The above code calls one method, I want it to call another one in from a different viewmodel.
Function call I want to add: model2.onButtonClick()
Is it possible? If yes, kindly add an minimal example. (Expecting a xml solution)
Note: Viewmodels are passed as arguments in xml. (data-binding variables)
Edit 1: Both functions are in two different view models. So I (after some research) know that using/declaring/initializing one view model inside another is not a good practise.
I could create a click listener inside my fragment and call both functions there. But I want to eliminate click listener in fragments (views as per MVVM).
I am not looking for some possible way. I am searching for a best practise method, where I intend to implement MVVM, seperation of concerns and data-binding.
Solution 1:[1]
I've found solution. Maybe stupid but genius in the simplicity:
app:onRefreshListener="@{() -> dashboardVM.onRefresh() != homeVM.onRefresh()}"
method needs to return true/false -> no matter.
For any other number of methods use OR and return always false.
Correct solution shall be implemented via converter. But it takes a lot of code.
Solution 2:[2]
You have two options,
- Call the second method in the first method.
fun method1() {
//do stuff
method2()
}
fun method2() {
//do more stuff
}
- In case you don't want to mess up your existing code structure, Create a separate function that calls the two functions.
fun onClickMethod () {
method1()
method2()
}
fun method1() {
//do stuff
}
fun method2() {
//do stuff
}
Solution 3:[3]
In your xml import two view models and pass them as params in your custom method which will call each method from the view models.
I have never try that before but it should work. Let me know if you don't understand
Solution 4:[4]
XML Implementation
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myFancyMethod" />
<!-- even more layout elements -->
Code Implementation
public void myFancyMethod(View v) {
// does something very interesting
}
Solution 5:[5]
Using a Higher-order function we can achieve trigger two methods at the same time
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="cartViewModel"
type="com.rizek.android.users.ui.cart.CartViewModel" />
<variable
name="product"
type="com.rizek.android.users.model.productlist.Product" />
<variable
name="productInteractionListener"
type="com.rizek.android.users.adapters.recyclerview.cart.ProductListAdapter.ProductInteractionListener" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
style="@style/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:onClick="@{()->
productInteractionListener.onItemStateChanged(product,cartViewModel.addProduct(product))
}"
android:padding="8dp"
android:text="Add to basket"
android:textColor="@color/blue"
android:textSize="14sp"
app:drawableStartCompat="@drawable/ic_add_circle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
ProductInteractionListener
interface ProductInteractionListener {
fun onItemStateChanged(product: Product, action: () -> Unit)
}
Solution 6:[6]
This work for me:
<Button
android:onClick="@{() -> model.onButtonClick() ? model2.onButtonClick() : void}"
... />
In fun onButtonClick() you must return True. From Google developer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2239604 |
| Solution 2 | |
| Solution 3 | Edgar Khimich |
| Solution 4 | A.J |
| Solution 5 | Arunachalam k |
| Solution 6 | Eneka |
