MVVMCross executing an ICommand on List item click.

Hey,

i need to execute an ICommand when a list item is clicked in my Mvx:ListView passing it the current object that list item is binded to.
I'm not sure how to do this as the ListView is already binded to an observable collection.

View Layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Mvx.MvxListView android:layout_width="match_parent" android:layout_height="match_parent" local:MvxItemTemplate="@layout/listitem_memorywall" local:MvxBind="ItemsSource Memories //do something here to pass current list item clicked to command." /> </LinearLayout>

List View Cell Template:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20dp" local:MvxBind="Text Name" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="15dp" local:MvxBind="Text Message" /> </LinearLayout>

Does anyone have any ideas?

Cheers,
Joe

Best Answer

Answers

  • TrevorBalcomTrevorBalcom USMember ✭✭

    You can use the ItemClick binding. For example:

    <Mvx.MvxListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        local:MvxItemTemplate="@layout/listitem_memorywall"
        local:MvxBind="ItemsSource Memories; ItemClick MemorySelectedCommand;" />
    

    Then in your ViewModel you would add a property like this:

        private MvxCommand<Memory> _memorySelectedCommand;
        public IMvxCommand MemorySelectedCommand
        {
            get { return _memorySelectedCommand ?? (_memorySelectedCommand = new MvxCommand<Memory>(OnMemorySelected)); }
        }
    
        void OnMemorySelected(Memory selectedMemory)
        {
        }
    
  • Ahad_khanAhad_khan USUniversity

    Will "selectedItem" work instead of "ItemClick" ?

  • ReizeiReizei CAMember

    How can I use ItemLongClick with ItemClick ?

    Ex:
    <Mvx.MvxListView android:layout_width="match_parent" android:layout_height="match_parent" local:MvxItemTemplate="@layout/listitem_memorywall" local:MvxBind="ItemsSource Memories; ItemClick MemorySelectedCommand; ItemLongClick MemoryLongClickCommand" />

  • BrianSunburyBrianSunbury USMember ✭✭

    ^^ Same. Has how can I use a long press as well?

  • BrianSunburyBrianSunbury USMember ✭✭

    Never mind they both work!

Sign In or Register to comment.