Last active
December 31, 2015 06:39
-
-
Save ChaseFlorell/7948654 to your computer and use it in GitHub Desktop.
Navigation Drawer Example
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
// THIS IS THE DRAWER TOGGLER CLASS THAT HANDLES THE TOGGLING. | |
public class ActionBarDrawerEventArgs : EventArgs | |
{ | |
public View DrawerView { get; set; } | |
public float SlideOffset { get; set; } | |
public int NewState { get; set; } | |
} | |
public delegate void ActionBarDrawerChangedEventHandler(object s, ActionBarDrawerEventArgs e); | |
public class DrawerToggler : ActionBarDrawerToggle | |
{ | |
public DrawerToggler ( Activity activity, DrawerLayout drawerLayout, int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes ) | |
: base(activity, drawerLayout, drawerImageRes, openDrawerContentDescRes, closeDrawerContentDescRes) | |
{ } | |
public event ActionBarDrawerChangedEventHandler DrawerClosed; | |
public event ActionBarDrawerChangedEventHandler DrawerOpened; | |
public event ActionBarDrawerChangedEventHandler DrawerSlide; | |
public event ActionBarDrawerChangedEventHandler DrawerStateChanged; | |
public override void OnDrawerClosed(View drawerView) | |
{ | |
if (null != DrawerClosed) | |
DrawerClosed(this, new ActionBarDrawerEventArgs { DrawerView = drawerView }); | |
base.OnDrawerClosed(drawerView); | |
} | |
public override void OnDrawerOpened(View drawerView) | |
{ | |
if (null != DrawerOpened) | |
DrawerOpened(this, new ActionBarDrawerEventArgs { DrawerView = drawerView }); | |
base.OnDrawerOpened(drawerView); | |
} | |
public override void OnDrawerSlide(View drawerView, float slideOffset) | |
{ | |
if (null != DrawerSlide) | |
DrawerSlide(this, new ActionBarDrawerEventArgs | |
{ | |
DrawerView = drawerView, | |
SlideOffset = slideOffset | |
}); | |
base.OnDrawerSlide(drawerView, slideOffset); | |
} | |
public override void OnDrawerStateChanged(int newState) | |
{ | |
if (null != DrawerStateChanged) | |
DrawerStateChanged(this, new ActionBarDrawerEventArgs | |
{ | |
NewState = newState | |
}); | |
base.OnDrawerStateChanged(newState); | |
} | |
} |
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
// THIS WORKS IF BOTH PORTRAIT AND LANDSCAPE LAYOUTS ARE USING A NAVIGATION DRAWER | |
// THE HAMBURGER MENU DISPLAYS EXACTLY AS EXPECTED | |
[Activity] | |
public class HomeView : MvxFragmentActivity | |
{ | |
private string _actionBarTitle; | |
private RelativeLayout _drawerInnerLayout; | |
private DrawerToggler _drawerToggle; | |
private DrawerLayout _navigationDrawer; | |
private ListView _topDrawerList; | |
private IList<NavigationItem> _navigationList; | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
SetContentView(Resource.Layout.Home_HomeView); | |
_navigationList = ((HomeViewModel)ViewModel).NavigationItems; | |
_actionBarTitle = Resources.GetString(Resource.String.ApplicationName); | |
_drawerInnerLayout = FindViewById<RelativeLayout>(Resource.Id.Home_HomeView_DrawerRelativeLayout); | |
_topDrawerList = FindViewById<ListView>(Resource.Id.Home_HomeView_TopDrawerList); | |
_topDrawerList.Adapter = new MenuListAdapter(this, _navigationList); | |
_topDrawerList.ItemClick += (sender, args) => SelectItem(args.Position); | |
_navigationDrawer = FindViewById<DrawerLayout>(Resource.Id.Home_HomeView_DrawerLayout); | |
_navigationDrawer.SetDrawerShadow(Resource.Drawable.drawer_shadow_dark, (int)GravityFlags.Start); | |
ActionBar.SetDisplayHomeAsUpEnabled(true); | |
ActionBar.SetHomeButtonEnabled(true); | |
//DrawerToggle is the animation that happens with the indicator next to the | |
//ActionBar icon. You can choose not to use this. | |
_drawerToggle = new DrawerToggler(this, | |
_navigationDrawer, | |
Resource.Drawable.ic_drawer_dark, | |
Resource.String.DrawerOpen, | |
Resource.String.DrawerClose); | |
//You can alternatively use _drawer.DrawerClosed here | |
_drawerToggle.DrawerClosed += delegate | |
{ | |
// Set the ActionBar Title to the current Chapter | |
ActionBar.Title = _actionBarTitle; | |
InvalidateOptionsMenu(); | |
}; | |
//You can alternatively use _drawer.DrawerOpened here | |
_drawerToggle.DrawerOpened += delegate | |
{ | |
// Set the ActionBar Title to the book name. | |
ActionBar.Title = Resources.GetString(Resource.String.Book_Title) + " Index"; | |
InvalidateOptionsMenu(); | |
}; | |
_navigationDrawer.SetDrawerListener(_drawerToggle); | |
if (null == bundle) | |
SelectItem(0); | |
} | |
private void SelectItem(int position) | |
{ | |
var fragment = new HomeFragment(); | |
var arguments = new Bundle(); | |
fragment.Arguments = arguments; | |
_actionBarTitle = ((HomeViewModel)ViewModel).NavigationItems[position].Text; | |
SupportFragmentManager.BeginTransaction() | |
.Replace(Resource.Id.Home_HomeView_ContentFrame, fragment) | |
.Commit(); | |
_topDrawerList.SetItemChecked(position, true); | |
ActionBar.Title = _actionBarTitle; | |
_navigationDrawer.CloseDrawer(_drawerInnerLayout); | |
} | |
protected override void OnPostCreate(Bundle savedInstanceState) | |
{ | |
base.OnPostCreate(savedInstanceState); | |
_drawerToggle.SyncState(); | |
} | |
public override void OnConfigurationChanged(Configuration newConfig) | |
{ | |
base.OnConfigurationChanged(newConfig); | |
_drawerToggle.OnConfigurationChanged(newConfig); | |
} | |
public override bool OnOptionsItemSelected(IMenuItem item) | |
{ | |
// Pass the event to ActionBarDrawerToggle, if it returns | |
// true, then it has handled the app icon touch event | |
if (_drawerToggle.OnOptionsItemSelected(item)) | |
{ | |
return true; | |
} | |
// Handle your other action bar items... | |
return base.OnOptionsItemSelected(item); | |
} | |
} |
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
// THIS DOES NOT WORK IF BOTH PORTRAIT IS USING THE NAVIGATION DRAWER AND LANDSCAPE USES A LINEAR LAYOUT | |
// THE MENU WORKS FINE, BUT THE HAMBURGER MENU DOESN'T DISPLAY. | |
[Activity] | |
public class HomeView : MvxFragmentActivity | |
{ | |
private string _actionBarTitle; | |
private RelativeLayout _drawerInnerLayout; | |
private DrawerToggler _drawerToggle; | |
private DrawerLayout _navigationDrawer; | |
private ListView _topDrawerList; | |
private IList<NavigationItem> _navigationList; | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
SetContentView(Resource.Layout.Home_HomeView); | |
_navigationList = ((HomeViewModel)ViewModel).NavigationItems; | |
_actionBarTitle = Resources.GetString(Resource.String.ApplicationName); | |
_drawerInnerLayout = FindViewById<RelativeLayout>(Resource.Id.Home_HomeView_DrawerRelativeLayout); | |
_topDrawerList = FindViewById<ListView>(Resource.Id.Home_HomeView_TopDrawerList); | |
_topDrawerList.Adapter = new MenuListAdapter(this, _navigationList); | |
_topDrawerList.ItemClick += (sender, args) => SelectItem(args.Position); | |
_navigationDrawer = FindViewById(Resource.Id.Home_HomeView_DrawerLayout) as DrawerLayout; | |
if (_navigationDrawer != null) | |
{ | |
_navigationDrawer.SetDrawerShadow(Resource.Drawable.drawer_shadow_dark, (int) GravityFlags.Start); | |
ActionBar.SetDisplayHomeAsUpEnabled(true); | |
ActionBar.SetHomeButtonEnabled(true); | |
//DrawerToggle is the animation that happens with the indicator next to the | |
//ActionBar icon. You can choose not to use this. | |
_drawerToggle = new DrawerToggler(this, | |
_navigationDrawer, | |
Resource.Drawable.ic_drawer_dark, | |
Resource.String.DrawerOpen, | |
Resource.String.DrawerClose); | |
//You can alternatively use _drawer.DrawerClosed here | |
_drawerToggle.DrawerClosed += delegate | |
{ | |
// Set the ActionBar Title to the current Chapter | |
ActionBar.Title = _actionBarTitle; | |
InvalidateOptionsMenu(); | |
}; | |
//You can alternatively use _drawer.DrawerOpened here | |
_drawerToggle.DrawerOpened += delegate | |
{ | |
// Set the ActionBar Title to the book name. | |
ActionBar.Title = Resources.GetString(Resource.String.Book_Title) + " Index"; | |
InvalidateOptionsMenu(); | |
}; | |
_navigationDrawer.SetDrawerListener(_drawerToggle); | |
} | |
if (null == bundle) | |
SelectItem(0); | |
} | |
private void SelectItem(int position) | |
{ | |
var fragment = new HomeFragment(); | |
var arguments = new Bundle(); | |
fragment.Arguments = arguments; | |
_actionBarTitle = ((HomeViewModel)ViewModel).NavigationItems[position].Text; | |
SupportFragmentManager.BeginTransaction() | |
.Replace(Resource.Id.Home_HomeView_ContentFrame, fragment) | |
.Commit(); | |
_topDrawerList.SetItemChecked(position, true); | |
ActionBar.Title = _actionBarTitle; | |
if (_navigationDrawer != null) | |
_navigationDrawer.CloseDrawer(_drawerInnerLayout); | |
} | |
protected override void OnPostCreate(Bundle savedInstanceState) | |
{ | |
base.OnPostCreate(savedInstanceState); | |
if (_drawerToggle != null) | |
_drawerToggle.SyncState(); | |
} | |
public override void OnConfigurationChanged(Configuration newConfig) | |
{ | |
base.OnConfigurationChanged(newConfig); | |
if (_drawerToggle != null) | |
_drawerToggle.OnConfigurationChanged(newConfig); | |
} | |
public override bool OnOptionsItemSelected(IMenuItem item) | |
{ | |
if (_drawerToggle != null) | |
{ | |
// Pass the event to ActionBarDrawerToggle, if it returns | |
// true, then it has handled the app icon touch event | |
if (_drawerToggle.OnOptionsItemSelected(item)) | |
{ | |
return true; | |
} | |
} | |
// Handle your other action bar items... | |
return base.OnOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment