質問: ((layoutinflater) this.getsystemservice(context.layout_inflater_service)).inflate( r.layout.drawer_list_footer, null, false); drawerlist.addfooterview(footerview); drawerlist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int pos, long arg3) { map.drawerclickevent(pos); } }); } @override public boolean onoptionsitemselected(menuitem item) { if (drawertoggle.onoptionsitemselected(item)) { return true; } return super.onoptionsitemselected(item); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); drawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); drawertoggle.onconfigurationchanged(newconfig); } } このアクティビティではnavigation drawerを使用したいので、 'navigationdrawer'を拡張します。他のアクティビティでは、同じnavigation drawerを使用します。 public class sampleactivity extends navigationdrawer {...} 何を変更すればよいのかわかりません....
回答: package xxxxxx; import android.app.searchmanager; import android.content.context; import android.content.intent; import android.widget.searchview; import android.support.design.widget.navigationview; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.toast; public class loginhome extends appcompatactivity { private toolbar toolbar; private navigationview navigationview; private drawerlayout drawerlayout; // make sure to be using android.support.v7.app.actionbardrawertoggle version. // the android.support.v4.app.actionbardrawertoggle has been deprecated. private actionbardrawertoggle drawertoggle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.loginhome); // initializing toolbar and setting it as the actionbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); //initializing navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); //setting navigation view item selected listener to handle the item click of the navigation menu navigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() { // this method will trigger on item click of navigation menu public boolean onnavigationitemselected(menuitem menuitem) { //checking if the item is in checked state or not, if not make it in checked state if(menuitem.ischecked()) menuitem.setchecked(false); else menuitem.setchecked(true); //closing drawer on item click drawerlayout.closedrawers(); //check to see which item was being clicked and perform appropriate action switch (menuitem.getitemid()){ //replacing the main content with contentfragment which is our inbox view; case r.id.nav_first_fragment: toast.maketext(getapplicationcontext(),"first fragment",toast.length_short).show(); firstfragment fragment = new firstfragment(); android.support.v4.app.fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.frame,fragment); fragmenttransaction.commit(); return true; // for rest of the options we just show a toast on click case r.id.nav_second_fragment: toast.maketext(getapplicationcontext(),"second fragment",toast.length_short).show(); secondfragment fragment2 = new secondfragment(); android.support.v4.app.fragmenttransaction fragmenttransaction2 = getsupportfragmentmanager().begintransaction(); fragmenttransaction2.replace(r.id.frame,fragment2); fragmenttransaction2.commit(); return true; default: toast.maketext(getapplicationcontext(),"somethings wrong",toast.length_short).show(); return true; } } }); // initializing drawer layout and actionbartoggle drawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle actionbardrawertoggle = new actionbardrawertoggle(this,drawerlayout,toolbar,r.string.drawer_open, r.string.drawer_close){ @override public void ondrawerclosed(view drawerview) { // code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank super.ondrawerclosed(drawerview); } @override public void ondraweropened(view drawerview) { // code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank super.ondraweropened(drawerview); } }; //setting the actionbartoggle to drawer layout drawerlayout.setdrawerlistener(actionbardrawertoggle); //calling sync state is necessay or else your hamburger icon wont show up actionbardrawertoggle.syncstate(); } toolbar.xmlにこれを使用する<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorprimary" android:elevation="4dp" android:id="@+id/toolbar" android:theme="@style/themeoverlay.appcompat.dark" > </android.support.v7.widget.toolbar> 使用したい場合はナビゲーションヘッダにこれを使用します<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="192dp" android:background="?attr/colorprimarydark" android:padding="16dp" android:theme="@style/themeoverlay.appcompat.dark" android:orientation="vertical" android:gravity="bottom"> <linearlayout android:layout_width="match_parent" android:layout_height="56dp" android:id="@+id/navhead" android:orientation="vertical" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true"> <textview android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="16dp" android:textcolor="#ffffff" android:text="tanya" android:textsize="14sp" android:textstyle="bold" /> <textview android:id="@+id/email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffffff" android:layout_marginleft="16dp" android:layout_margintop="5dp" android:text="tanya.com" android:textsize="14sp" android:textstyle="normal" /> </linearlayout> <de.hdodenhof.circleimageview.circleimageview android:layout_width="70dp" android:layout_height="70dp" android:layout_below="@+id/imageview" android:layout_margintop="15dp" android:src="@drawable/face" android:id="@+id/circleview" /> </relativelayout>