android - How to disable manual sliding-out of navigation view -
i have 2 navigation views in activity. 1 enters right , other enters left.
in navigtionview enters from left, different fragments started when when items clicked. , also, same navigationview has menu items common launched fragments. don't have problem one.
now, navigationview enters right has menu items peculiar particular fragment started when first item in left entering navigationview clicked. means that, when clicked first item in left entering navigation drawer, fragament started, , items in right entering navigationview has items related fragment.
so, right navigationview stared when menuitem in toolbar clicked. , menu item not visible when other fragments (apart aforementioned) in view.
the problem have that, when right entering navigationview cannot launched through menuitem in other fragments, can still started sliding right edge of screen. want totally disable sliding feature of right entering navigationview, can launched when menu item clicked.
codes
activity_main
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout android:id="@+id/drawer_layout" 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:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:opendrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent"/> <android.support.design.widget.navigationview android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="false" app:headerlayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/> <android.support.design.widget.navigationview android:id="@+id/cat_nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="end" android:fitssystemwindows="false"> /** navigationview enters right, start fragment framelayout below. fragment contains recyclerview **/ <framelayout android:id="@+id/transport_cat_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.design.widget.navigationview> </android.support.v4.widget.drawerlayout>
snippets of mainactivity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.bind(this); actionbardrawertoggle mdrawertoggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close) { @override public void ondrawerclosed(view v){ super.ondrawerclosed(v); } @override public void ondraweropened(view v) { super.ondraweropened(v); } }; drawer.adddrawerlistener(mdrawertoggle); mdrawertoggle.syncstate(); navigationview.setnavigationitemselectedlistener(this); } @override public void onbackpressed() { if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else if (drawer.isdraweropen(gravitycompat.end)) { drawer.closedrawer(gravitycompat.end); } else { super.onbackpressed(); } } actionbardrawertoggle mdrawertoggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close) { @override public void ondrawerclosed(view v){ super.ondrawerclosed(v); } @override public void ondraweropened(view v) { super.ondraweropened(v); } }; drawer.adddrawerlistener(mdrawertoggle); mdrawertoggle.syncstate(); navigationview.setnavigationitemselectedlistener(this); override public boolean onnavigationitemselected(menuitem item) { // handle navigation view item clicks here. fragment fragment; if (id == r.id.menu_cars) { fragment = new carsfragment(); startcarsfrag() //method start carsfragment() //the right entering drawer should enabled fragment } if (id == r.id.menu_trains) { fragment = new trainsfragment(); starttrainfrag() //method start trainsfragment } if (id == r.id.menu_lorries) { fragment = new lorriesfragment(); startlorriesfrag() //method start lorriesfragment } if (drawer != null) { drawer.closedrawer(gravitycompat.start); } return true; }
the drawerlayout#setdrawerlockmode()
method you're looking for. when locked, drawer view
cannot dragged open/closed, though still respond opendrawer()
, closedrawer*()
methods.
since you're using 2 drawers, , want lock one, you'll need call method second argument indicate drawer lock/unlock. example, lock secondary drawer closed:
drawer.setdrawerlockmode(drawerlayout.lock_mode_locked_closed, gravitycompat.end);
Comments
Post a Comment