Disabling the navigation box, turning on the up / down button in fragments

Customization

I have an action whose contentView is an instance of DrawerLayout , in which there is a navigation box with a drawer indicator displayed on the action bar. The action contains a Fragment , name it ListFragment , which contains a list of options. When this option is clicked, I replace the ListFragment with DetailFragment .

Architecture of the application

At this point, I would like to display the “up” navigation option instead of the navigation box indicator. I can display the up icon if I turn off the drawer indicator by calling mDrawerToggle.setDrawerIndicatorEnabled(false) , but this only removes the drawer icon - it does not remove functionality, that is, when I click on the carriage, the navigation box is still open.

Also, in these subviews, I would like to disable the opening of the drawer by dragging it from the edge of the screen. I tried to do this by calling setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) , but it did not seem to disable this function.

I tried to extend the ActionBarDrawerToggle class to prevent the box from opening when the indicator clicks - however, all that happens is that the override is performed ("up"), but the box still opens .

I also followed the steps in Switching between Android Navigation Drawer and Up caret images when using fragments . It works, because the carriage display is in progress, but despite the cancellation of the functionality of the up button, the menu still opens (the application moves backward - it just also opens the box).

Question

So, a long story: is there (preferably clean and elegant, but at the moment I will go with a hacker) way to achieve these things, when my DrawerLayout layout DrawerLayout :

  • Replace the drawer indicator with the carriage "up" (after doing mDrawerToggle.setDrawerIndicatorEnabled(false))
  • Prevent the drawer from opening when I click on the carriage and instead redefine my own "up" functionality
  • Prevent the drawer from opening when dragging from the edge of the screen.

Edit

Everything is fine, it seems that if I redefine ActionBarDrawerToggle AND onOptionsItemSelected , the menu does not open when I click on the caret. But it still opens if I drag it from the edge. Help!

+43
android android-actionbar navigation-drawer
Oct 17 '13 at 23:58
source share
5 answers

This is only part of the solution I came to, but it was rather difficult to understand this error, so I leave it here for posterity.

This is how I defined the ListView for my navigation box:

 <ListView android:id="@+id/listview_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start|bottom" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> 

Even after calling setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) I could still open the slider.

However, after changing layout_gravity to "start" this problem seems to be resolved.

I was able to reproduce this problem in a sample, navigation box-only application, so this seems like a reproducible problem, not unique to my situation.

+21
Oct 28 '13 at 22:09
source share

Shortcode

 public void setDrawerState(boolean isEnabled) { if ( isEnabled ) { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); drawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_UNLOCKED); drawerToggle.setDrawerIndicatorEnabled(true); drawerToggle.syncState(); } else { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); drawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); drawerToggle.setDrawerIndicatorEnabled(false); drawerToggle.syncState(); } } 
+31
Apr 28 '15 at 16:35
source share

Based on sonida's answer. After calling setDrawerIndicatorEnabled (false), onNavigateUp has not yet been called. So, I just created a new onClickListener that called it:

 public void setDrawerState(boolean isEnabled) { if ( isEnabled ) { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); drawerToggle.setDrawerIndicatorEnabled(true); drawerToggle.syncState(); } else { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); drawerToggle.setDrawerIndicatorEnabled(false); drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onSupportNavigateUp(); } }); drawerToggle.syncState(); } } 

Also i think

 drawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_UNLOCKED); 

was discounted, but it works great without it.

+9
Aug 07 '15 at 21:06
source share

You need to disable the swipe and disable the home button of the action bar:

Use the code below, which is based on the code already specified to disable scrolling

  public void setDrawerState(boolean isEnabled) { if ( isEnabled ) { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); mDrawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_UNLOCKED); mDrawerToggle.setDrawerIndicatorEnabled(true); mDrawerToggle.syncState(); getActivity().getActionBar().setHomeButtonEnabled(true); } else { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); mDrawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); mDrawerToggle.setDrawerIndicatorEnabled(false); mDrawerToggle.syncState(); getActivity().getActionBar().setHomeButtonEnabled(false); } } 
+2
Oct 25 '15 at 7:34
source share

Based on @sonida's answer And after using the settings given by @ luca992 and @jai.

I tried the suggested codes above. But the up or back arrow on the left side of the action bar just didn't appear in my application. But, fortunately, I was able to fix it.

I had to add this extra line of code to setNavigationDrawerState () [Ref: android.support.v7.app.ActionBarDrawerToggle.setHomeAsUpIndicator ]

toggle.setHomeAsUpIndicator (R.drawable.ic_keyboard_backspace_white_24dp);

I downloaded drawable: ic_keyboard_backspace_white_24dp from Material.io

Here is the complete code:

MainActivity.java → onCreate ()

 DrawerLayout drawer; ActionBarDrawerToggle toggle; @Override protected void onCreate(Bundle savedInstanceState) { // Start: Code automatically generated by Android Studio super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); // End: Code automatically generated by Android Studio // I had to add this listener as the "back" arrow was totally unresponsive // Thanks to @luca992 toggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); // Start: Code automatically generated by Android Studio NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); // End: Code automatically generated by Android Studio // More custom code for other stuff // ... } 

MainActivity.java → setNavigationDrawerState ()

 public void setNavigationDrawerState(boolean isEnabled) { if ( isEnabled ) { drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); toggle.setDrawerIndicatorEnabled(true); toggle.syncState(); } else { drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); toggle.setDrawerIndicatorEnabled(false); // the extra line of code goes here toggle.setHomeAsUpIndicator(R.drawable.ic_keyboard_backspace_white_24dp); toggle.syncState(); } 

MainActivity.java → onBackPressed ()

 @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else if(getSupportFragmentManager().getBackStackEntryCount() > 0){ getSupportFragmentManager().popBackStack(); }else { super.onBackPressed(); } } 

MainActivity.java → startFragment () [for example, a dummy function]

 public void startFragment(){ MyFrag myFrag = new MyFrag(); getSupportFragmentManager() .beginTransaction() .replace(R.id.frag_container ,myFrag) .addToBackStack(null) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .commit(); } 

MyFrag.java → onViewCreated ()

 @Override public void onViewCreated (View view, Bundle savedInstanceState){ super.onViewCreated(view, savedInstanceState); // Say, using an implemented interface Make call to MainActivitiy setNavigationDrawerState() passing false // setNavigationDrawerState(false) // ... } 

MyFrag.java → onDestroyView ()

 @Override public void onDestroyView(){ // Say, using an implemented interface Make call to MainActivitiy setNavigationDrawerState() passing true // setNavigationDrawerState(true) super.onDestroyView(); } 
0
Feb 12 '17 at 12:06 on
source share



All Articles