The integration is still a work in progress, but you can see some Fragment binding in https://github.com/slodge/FragmentSample/
This sample shows some basic principles:
- A fragment declared in AXML and it's DataContext (ViewModel) set in Code Behind
This file contains hidden or 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
var listFragment = (MyListFragment) SupportFragmentManager.FindFragmentById(Resource.Id.list_fragment); | |
listFragment.ViewModel = ViewModel; |
This file contains hidden or 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
<fragment class="FragmentSample.UI.Droid.Views.MyListFragment" | |
android:id="@+id/list_fragment" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
/> |
- Fragment switched in/out of a ViewGroup container programmatically using Code Behind
This file contains hidden or 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
_firstFragment = new MyFirstFragment() | |
{ | |
ViewModel = ViewModel | |
}; | |
ShowFormFragment(_firstFragment); | |
private void ShowFormFragment(MvxFragment myFragment) | |
{ | |
var t = SupportFragmentManager.BeginTransaction(); | |
t.Replace(Resource.Id.titles_fragment_holder, myFragment); | |
t.Commit(); | |
} |
This file contains hidden or 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
<FrameLayout | |
android:id="@+id/titles_fragment_holder" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
/> |
- Fragments loaded into Tabs
This file contains hidden or 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
[Activity] | |
public class TabsFragmentActivity : MvxTabsFragmentActivity | |
{ | |
public new TabViewModel ViewModel | |
{ | |
get { return (TabViewModel)base.ViewModel; } | |
set { base.ViewModel = value; } | |
} | |
public TabsFragmentActivity() | |
: base(Resource.Layout.Page_TabsView, Resource.Id.actualtabcontent) | |
{ | |
} | |
protected override void AddTabs(Bundle args) | |
{ | |
AddTab<Tab1Fragment>("Tab1", "Tab1", "Tab 1", args, ViewModel.Vm1); | |
AddTab<Tab2Fragment>("Tab2", "Tab2", "Tab 2", args, ViewModel.Vm2); | |
AddTab<Tab3Fragment>("Tab3", "Tab3", "Tab 3", args, ViewModel.Vm3); | |
AddTab<Tab3Fragment>("Tab4", "Tab4", "Tab 4", args, ViewModel.Vm3); | |
} | |
} |
- Fragments used in Dialogs
This file contains hidden or 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 FirstDialogFragment : MvxDialogFragment | |
{ | |
private readonly Context _context; | |
public FirstDialogFragment(Context context) | |
{ | |
_context = context; | |
} | |
public override Dialog OnCreateDialog(Bundle savedState) | |
{ | |
base.EnsureBindingContextSet(savedState); | |
var view = this.BindingInflate(Resource.Layout.Dialog_FooBar, null); | |
var dialog = new AlertDialog.Builder(_context); | |
dialog.SetTitle("First Dialog"); | |
dialog.SetView(view); | |
dialog.SetNegativeButton("Cancel", (s, a) => { }); | |
dialog.SetPositiveButton("OK", (s, a) => { }); | |
return dialog.Create(); | |
} | |
} |
----
There's still plenty more to learn and explore in these Fragments - the lifecycle of fragments really is a bit odd... so this code isn't really fully production ready yet...
There's also quite a few other things to try - like maps, view pagers and bound fragment collections...
But at least fragments can now be used and can be easily data-bound :)
No comments:
Post a Comment