Creating and using fragments
Sometimes normal layouts aren't dynamic enough or we may have to reuse sections of the UI and its related code.
Getting ready
To add fragments to our app we need to be targeting Android version 3.0 and above; for versions below that, we need to have installed the Xamarin.Android.Support.v4 package.
How to do it...
In order to make use of fragments in our apps, we need to create a new fragment type. Then, either in the layout or in the code, we can insert the fragments into their appropriate places in the UI. Let's take a look at the following steps:
To make a master-detail app, we need a menu or list of items to select. For the menu fragment we are going to inherit from
ListFragment
, which is very similar toListActivity
, and set theListAdapter
property:public class MenuFragment : ListFragment { public override void OnStart() { base.OnStart(); ListAdapter = new ArrayAdapter( Activity, Android.Resource.Layout.SimpleListItem1, new [] {...