This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Profile : IEnumerable<Device> |
Here is my Activity with the the grouped list declared
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:centrastage="http://schemas.android.com/apk/res/CentraStage.Droid" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<DeapExtensions.Binding.Droid.Views.BindableGroupListView | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" | |
centrastage:MvxItemTemplate="@layout/listitem_device" | |
centrastage:GroupItemTemplate="@layout/listitem_profile" | |
centrastage:MvxBind="{'ItemsSource':{'Path':'SearchedProfiles'},'ItemClick':{'Path':'DeviceSelected'}}" /> | |
</LinearLayout> |
The important part is the MvxBind which binds the ListView to the SearchedProfiles property in my ViewModel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DeviceBrowserVM : BaseViewModel | |
{ | |
public ObservableCollection<Profile> SearchedProfiles | |
{ | |
get{ | |
return searchedDevices; | |
} | |
private set{ | |
searchedDevices = value; | |
FirePropertyChanged("SearchedProfiles"); | |
} | |
} | |
} |
The Item and Group Template are regular Android Layouts containing TextViews. These TextViews are bound using MvvmCross directly to there respective model objects. So list item_profile was bound to the ProfileName property on Profile and listitem_device which has 2 TextViews was bound to Device and its corresponding propertys.
BindableGroupListView works by assuming the list of items you bind to it are groups and that these groups which are are enumerable contain the items you wish to see in the group.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public MvxRelayCommand<Device> DeviceSelected { | |
get { | |
return new MvxRelayCommand<Device> ((device) => | |
{ | |
DataStore.AddToRecentlyUsed(device); | |
this.RequestNavigate<DeviceVM>(new { deviceUid = device.Id.ToString() }, true, new MvxRequestedBy()); | |
}); | |
} | |
} |
Lastly, notice how I also declare the ItemClick to DeviceSelected in the bind command. DeviceSelected is an ICommand declared in the ViewModel. DeviceSelected gets the Device selected and navigates to another Activity.
5 comments:
Thanks for the blog + code.
For others trying this, some tips:
- Link this project in your own solution
- Derive your setup from the abstract setup class of DeapExtensions
- Copy one line from the Attributes XML file to your own Android project xml file.
- Make sure your group-level derives from IEnumerable. You can also just implement IEnumrator and fill in GetEnumerator to use some other property of your group class.
This will help you speedup using the code.
Kevin, thanks for the blog entry, this is proving very useful for my current project. I am however a little confused on exactly what the full ViewModel should look like with your example. Any chance you could post the full viewmodel for this example?
Thanks, I can provide an email if that would be easier.
Hi,
Under was licence are your MvvmCross.DeapExtensions published?
Really great, I have started to port my WP8 Project to Xamarin and this code is really neccesary to implement the longlistselector in Android, Thanks a lot.
Post a Comment