Reputation: 33
I'm building an app on Android via Xamarin using MvvmCross.
I've been struggling on this issue for some time, and I have found a work-around, but I'm pretty sure this isn't the right way to do it.
It concerns the selected item in a MvxListView:
<Mvx.MvxListView
android:id="@+id/productSearchResultListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="ItemsSource ProductSearchResultLst; SelectedItem SelectedProductSearchResult"
local:MvxItemTemplate="@layout/productsearchresult_row" />
When an item of this list is selected, the user is fowarded to a page where this item is handled. Once this is done, the user clicks 'confirm' and returns to this list. At this point, the selected item is highlighted (which I like), but it cannot be clicked again. That is, the item is not opened if you click on it again. I do understand why. There is no selected item change. However, I can't seem to unselect the viewmodel's selected item SelectedProductSearchResult". I've put it to 'null' on 'OnRestart' event coming from the view. However the setter of the selected item is not reached when selecting the item again.
I've also played with ClearChoices and SetItemChecked on the view directly, but this doesn't unselect my VM's selected item, and therefore doesn't help me.
Finally, I've figured out that the selected item is unchanged (same object) and therefore the setter is not reached. I now included the following piece on the OnRestart method of the viewmodel, which renews the list in that sense that all the items are new objects:
var productLst = new List<WHM_PRODUCT> ();
foreach (var item in ProductSearchResultLst)
productLst.Add (item.Clone());
ProductSearchResultLst = new ObservableCollection<WHM_PRODUCT> (productLst);
This works, but I think this is very stupid, because it would mean that I have to do this for each object seperately.
There must be a better way to do this.
Any feedback is much appreciated!
Stefaan
Upvotes: 3
Views: 1554
Reputation: 1097
You didn't include your viewmodel's code but I belive you execute your navigation logic in setter of your SelectedProductSearchResult
property.
That's not the standard approach and you should rather use Command.
First implement Command property in your viewmodel:
public ICommand NavigateToDetailCommand
{
get
{
return new MvxCommand<WHM_PRODUCT>(item =>
{
//Perform navigation logic here
ShowViewModel<DetailViewModel>(new { id = item.Id });
});
}
}
Then bind it to ItemClick on your MvxListView:
local:MvxBind="ItemsSource ProductSearchResultLst; ItemClick NavigateToDetailCommand"
Now whenewer user clicks on the list item, your command is executed and related item is passed as a parameter.
Upvotes: 4