c# - Android Xamarin NavigationView handling event error -
i working on navigationview make easy menu following tutorial : http://maximeesprit.com/en/xamarin-android-actionbar-en/
but have problem following menu items don't send events
my menu looks :
and main.xml code :
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" android:id="@+id/maintactivity_drawerlayout"> <android.support.design.widget.navigationview android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:id="@+id/maintactivity_navigationview" app:menu="@menu/left_menus" /> <linearlayout android:layout_width="match_parent" android:layout_height="240dp" android:orientation="vertical" android:gravity="bottom" android:layout_marginleft="16dp" android:id="@+id/container_layout"> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/framelayout_viewforfragments" /> </linearlayout> </android.support.v4.widget.drawerlayout>
and mainactivity.cs file :
using android.app; using android.widget; using android.os; using android.support.v7.app; using android.views; using android.support.v4.widget; using android.support.design.widget; using android.support.v4.view; // install nuget android.support.design // , app compat support namespace app4 { [activity(label = "topsidebar app", mainlauncher = true , icon = "@drawable/ic_launcher")] public class mainactivity : appcompatactivity { drawerlayout m_drawerlayout; navigationview m_navigationview; protected override void oncreate(bundle savedinstancestate) { base.oncreate(savedinstancestate); // set our view "main" layout resource setcontentview(resource.layout.main); supportactionbar.title = resources.getstring(resource.string.app_name); ; m_drawerlayout = findviewbyid<drawerlayout>(resource.id.maintactivity_drawerlayout); m_navigationview = findviewbyid<navigationview>(resource.id.maintactivity_navigationview); m_navigationview.navigationitemselected += m_navigationview_navigationitemselected; //display sandwich menu icon supportactionbar.setdisplayhomeasupenabled(true); supportactionbar.sethomeasupindicator(resource.drawable.ic_menu_black_24dp_1x); configurenavigationviewheader(); } private void configurenavigationviewheader() { view viewheader = layoutinflater.inflate(resource.layout.mainactivity_navigationview_header, null); imageview imageviewuseravatar = viewheader.findviewbyid<imageview>(resource.id.mainactivity_navigationview_header_imageview_useravatar); textview textviewuserpseudo = viewheader.findviewbyid<textview>(resource.id.mainactivity_navigationview_header_textview_userpseudo); textview textviewuserdesc = viewheader.findviewbyid<textview>(resource.id.mainactivity_navigationview_header_textview_userdesc); imageviewuseravatar.setimageresource(resource.drawable.profile_pic); textviewuserpseudo.text = "housma mardini"; textviewuserdesc.text = "web developer"; m_navigationview.addheaderview(viewheader); } private void m_navigationview_navigationitemselected(object sender, navigationview.navigationitemselectedeventargs e) { switch (e.menuitem.itemid) { case resource.id.leftmenu_profile: toast.maketext(this, "profile", toastlength.short).show(); m_drawerlayout.closedrawer(gravitycompat.start); break; case resource.id.leftmenu_shopping_list: break; case resource.id.leftmenu_favorites: break; case resource.id.leftmenu_params_option: break; case resource.id.leftmenu_params_about: break; } } public override bool oncreateoptionsmenu(android.views.imenu menu) { menuinflater.inflate(resource.menu.top_menus, menu); return base.oncreateoptionsmenu(menu); } public override bool onoptionsitemselected(imenuitem item) { switch (item.itemid) { case resource.id.share: toast.maketext(this , "share" ,toastlength.short ).show(); break; case resource.id.moreoption: toast.maketext(this, "settings", toastlength.short).show(); break; //handle sandwich menu icon click case android.resource.id.home: //if menu open, close it. else, open it. if (m_drawerlayout.isdraweropen(gravitycompat.start)) m_drawerlayout.closedrawers(); else m_drawerlayout.opendrawer(gravitycompat.start); break; } return base.onoptionsitemselected(item); } } }
but when remove linearlayout , framelayout tags main.xml file every thing working
as discussed mail, here answer :
your activity axml :
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" android:id="@+id/maintactivity_drawerlayout"> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/framelayout_viewforfragments" /> <android.support.design.widget.navigationview android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:id="@+id/maintactivity_navigationview" app:menu="@menu/left_menus" /> </android.support.v4.widget.drawerlayout>
Comments
Post a Comment