0
votes

I use MvvmCross for Android and I've created a ViewModel and want to pass tag attribute to ViewModel. How can I do it?

Here is example:

<LinearLayout
android:id="@+id/productsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/products_category"
android:gravity="center"
android:orientation="vertical"
android:tag="93ada18e-1280-4a80-bc39-9b9f6d5b9724"
local:MvxBind="Click ShowCategoryCommand">

There is could be few layouts with hardcoded tags attributes and I need detect which one was tapped and send tag to another ViewModel

2
You could register to the android:onClick event to detect which layout has been tapped - Hichame Yessou

2 Answers

2
votes

The simple answer should be:

local:MvxBind="Click ShowCategoryCommand, CommandParameter=93ada18e-1280-4a80-bc39-9b9f6d5b9724"

This problem is probably better solved using MvxRecyclerView. You can build a list of view models that are bound to the MvxRecyclerView and then use an MvxAsyncCommand<MyItemViewModel> to know what view model was selected.

<MvxRecyclerView
local:MvxBind="ItemsSource MyList; ItemClick MyModelClicked" 
local:MvxItemTemplate="@layout/mymodelitemview" />

Here's the nuget package for MvxRecyclerView: https://www.nuget.org/packages/MvvmCross.Droid.Support.V7.RecyclerView

0
votes

you can try something like this. This will works but I don't know if it's best solution.

// in OnCreateView method in your Fragment
var productLayout = _view.FindViewById<LinearLayout>(Resource.Id.productsLayout);
productLayout.Click += OnClick;

// OnClick method in your Fragment
private void OnClick(object sender, EventArgs e)
{
    var linearLayout = (LinearLayout)sender;
    var tag = linearLayout.Tag;
    ViewModel.ShowCategoryCommand(tag);
}

// ShowCategory method in your ViewModel
public void ShowCategoryCommand(string tag)
{
    // some code...
}