android - How to Disable material design Fragment Navigation Drawer inside a particular Activity -
i used fragment navigation drawer , floatingactionbutton inside application. clicking on floatingactionbutton takes me new activity. have added setdisplayhomeasupenabled(true) , takes me parent activity. inside activity still ableto swipe fragment navigaion drawer , when click on drawer items app crashes. want disablethe fragment navigation drawer inside second activity. how this. here code.
mainactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); getsupportactionbar().setdisplayshowhomeenabled(true); drawerfragment = (fragmentdrawer) getsupportfragmentmanager().findfragmentbyid(r.id.fragment_navigation_drawer); drawerfragment.setup(r.id.fragment_navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout), mtoolbar); drawerfragment.setdrawerlistener(this); imageview imageview = new imageview(this); imageview.setimageresource(r.mipmap.icon_launcher); floatingactionbutton actionbutton = new floatingactionbutton.builder(this).setcontentview(imageview).build(); actionbutton.setonclicklistener(this); } @override public void onclick(view v) { intent intent = new intent(this,addtopicactivity.class); startactivity(intent); }
activity_main.xml
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <linearlayout android:id="@+id/container_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> </linearlayout> <framelayout android:id="@+id/container_body" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" />` </linearlayout> <fragment android:id="@+id/fragment_navigation_drawer" android:name="mobi.com.swachapp.fragmentdrawer" android:layout_width="@dimen/nav_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" app:layout="@layout/fragment_navigation_drawer" tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.drawerlayout>
fragmentdrawer.java
public class fragmentdrawer extends fragment { private static string tag = fragmentdrawer.class.getsimplename(); private recyclerview recyclerview; private actionbardrawertoggle mdrawertoggle; private drawerlayout mdrawerlayout; private navigationdraweradapter adapter; private view containerview; private static string[] titles = null; private fragmentdrawerlistener drawerlistener; public fragmentdrawer() { } public void setdrawerlistener(fragmentdrawerlistener listener) { this.drawerlistener = listener; } public static list<navdraweritem> getdata() { list<navdraweritem> data = new arraylist<>(); int[] icons = {r.drawable.home_icon,r.drawable.usersignin,r.drawable.savedarticles,r.drawable.addtopic,r.drawable.trending,r.drawable.photography,r.drawable.photos,r.drawable.video,r.drawable.survey}; // preparing navigation drawer items (int = 0; < titles.length && < icons.length; i++) { navdraweritem navitem = new navdraweritem(); navitem.settitle(titles[i]); navitem.iconid = icons[i]; data.add(navitem); } return data; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // drawer labels titles = getactivity().getresources().getstringarray(r.array.nav_drawer_labels); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflating view layout view layout = inflater.inflate(r.layout.fragment_navigation_drawer, container, false); recyclerview = (recyclerview) layout.findviewbyid(r.id.drawerlist); adapter = new navigationdraweradapter(getactivity(), getdata()); recyclerview.setadapter(adapter); recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); recyclerview.addonitemtouchlistener(new recyclertouchlistener(getactivity(), recyclerview, new clicklistener() { @override public void onclick(view view, int position) { drawerlistener.ondraweritemselected(view, position); mdrawerlayout.closedrawer(containerview); } @override public void onlongclick(view view, int position) { } })); return layout; } public void setup(int fragmentid, drawerlayout drawerlayout, final toolbar toolbar) { containerview = getactivity().findviewbyid(fragmentid); mdrawerlayout = drawerlayout; mdrawertoggle = new actionbardrawertoggle(getactivity(), drawerlayout, toolbar, r.string.drawer_open, r.string.drawer_close) { @override public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); getactivity().invalidateoptionsmenu(); } @override public void ondrawerclosed(view drawerview) { super.ondrawerclosed(drawerview); getactivity().invalidateoptionsmenu(); } @override public void ondrawerslide(view drawerview, float slideoffset) { super.ondrawerslide(drawerview, slideoffset); toolbar.setalpha(1 - slideoffset / 2); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); mdrawerlayout.post(new runnable() { @override public void run() { mdrawertoggle.syncstate(); } }); } public static interface clicklistener { public void onclick(view view, int position); public void onlongclick(view view, int position); } static class recyclertouchlistener implements recyclerview.onitemtouchlistener { private gesturedetector gesturedetector; private clicklistener clicklistener; public recyclertouchlistener(context context, final recyclerview recyclerview, final clicklistener clicklistener) { this.clicklistener = clicklistener; gesturedetector = new gesturedetector(context, new gesturedetector.simpleongesturelistener() { @override public boolean onsingletapup(motionevent e) { return true; } @override public void onlongpress(motionevent e) { view child = recyclerview.findchildviewunder(e.getx(), e.gety()); if (child != null && clicklistener != null) { clicklistener.onlongclick(child, recyclerview.getchildposition(child)); } } }); } @override public boolean onintercepttouchevent(recyclerview rv, motionevent e) { view child = rv.findchildviewunder(e.getx(), e.gety()); if (child != null && clicklistener != null && gesturedetector.ontouchevent(e)) { clicklistener.onclick(child, rv.getchildposition(child)); } return false; } @override public void ontouchevent(recyclerview rv, motionevent e) { } } public interface fragmentdrawerlistener { public void ondraweritemselected(view view, int position); } }
addtopicactivity.java
public class addtopicfragment extends actionbaractivity { private toolbar mtoolbar; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_ma); setcontentview(r.layout.activity_main); mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); getsupportactionbar().sethomebuttonenabled(true); getsupportactionbar().setdisplayhomeasupenabled(true); } }
i want when activity starts fragment navigation drawer functionalities disabled completely. able remove drawer icon still when swipe left drawer comes picture.
thanks.
add inside addtopicactivity
second activity. lock swipe
for lock
mdrawerlayout.setdrawerlockmode(drawerlayout.lock_mode_locked_closed);
for unlock
mdrawerlayout.setdrawerlockmode(drawerlayout.lock_mode_unlocked);
Comments
Post a Comment