Reputation: 11
I would like to create re usable icon menu made of the TmageViews. I have been trying to find some good examples to follow, however I am new to Xamarin and C# development and I didn't find many.
My Project
I have created the same menu pattern which is included into every page with the include tag and is being used across the pages.
Every of these pages have got different its own layout and have been defined as separate activity.
The menu works but I am redefining the same elements over and over which is I think is necessary.
Please see the code below:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.AirConPage);
ImageView LightCat_btn = FindViewById<ImageView> (Resource.Id.Lights);
LightCat_btn.Click += new EventHandler (LightBtn_Click);
ImageView BlindCat_btn = FindViewById<ImageView> (Resource.Id.Blinds);
BlindCat_btn.Click += new EventHandler (BlindsBtn_Click);
void LightBtn_Click(object Sender, EventArgs e) {
Intent i = new Intent();
i.SetClass(this, typeof(LightPage));
// i.AddFlags(ActivityFlags.NewTask);
StartActivity(i);
Finish();
}
void BlindsBtn_Click(object Sender, EventArgs e) {
Intent i = new Intent();
i.SetClass(this, typeof(BlindPage));
// i.AddFlags(ActivityFlags.NewTask);
StartActivity(i);
Finish();
}
I would like to create external class for the menu where I would define the ImageView objects once and I could load them in every page of my project by inheritance or another way however I don't know how to achieve that.
I would be really grateful for any help, suggestions or links.
If you have any questions or if you need more information please post them.
Thanks in advance.
Upvotes: 1
Views: 1098
Reputation: 1414
My suggestion would be to make your menu an Android Fragment. Xamarin has a nice walkthrough tutorial: http://docs.xamarin.com/guides/android/platform_features/fragments/fragments_walkthrough
With Fragments you can replace a part of your layout using a Fragment Transaction:
var menuFragment = new MenuFragment();
var transaction = FragmentManager.BeginTransaction ();
transaction.Replace (Resource.Id.menuFrame, menuFragment);
transaction.Commit ();
For your menu fragment, you would create an Android Fragment that extends Fragment. You can then override the onCreateView method to set the view contents:
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
inflater.Inflate (Resource.Layout.MyMenu, container);
return base.OnCreateView (inflater, container, savedInstanceState);
}
Inside your Fragment class you can have an Event that the parent Activity listens to.
Another option you can do, is define a layout for you menu and "include" it in your other layouts:
http://developer.android.com/training/improving-layouts/reusing-layouts.html
Upvotes: 2