Andriod navigation drawer, no call button working.



  • There's no menu button in the left upper corner of the application after I switch to another escen in the menu.

    At the start of the application, one of the fragments of the application shall be default, and at this point, if you press the button that causes menu, the menu will come out of the left. After switching to another piece of this menu, the button doesn't respond to the pressure and the menu from the left, but it's a leva swab from the left to the right.

    I'd like to know why and where the problem is?

    Androidstudio was the very app.

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
    &lt;group android:checkableBehavior="single"&gt;
        &lt;item android:id="@+id/my_audios" android:icon="@drawable/ic_my_audio"
            android:title="Мои аудиозаписи"
            android:checked="true" /&gt;
        &lt;item android:id="@+id/popular_audios" android:icon="@drawable/ic_popular_audio"
            android:title="Популярные" /&gt;
        &lt;item android:id="@+id/recommend_audios" android:icon="@drawable/ic_recomendation_audio"
            android:title="Рекомендуемые" /&gt;
    &lt;/group&gt;
    
    &lt;item android:title="Проигрыватель"&gt;
        &lt;menu&gt;
            &lt;!--item android:id="@+id/clear_cash" android:icon="@android:drawable/ic_menu_share"
                android:title="Отчистить кэш" /--&gt;
            &lt;item android:id="@+id/item_player" android:icon="@drawable/ic_player_audio"
                android:title="Открыть" /&gt;
            &lt;!--item android:id="@+id/exit_from_profile" android:icon="@android:drawable/ic_menu_send"
                android:title="Выйти из профиля" /--&gt;
        &lt;/menu&gt;
    &lt;/item&gt;
    

    </menu>

    MainActivity

    SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
    Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mActionBarToolbar);
    // Handle navigation view item clicks here.
    int id = item.getItemId();

        if (id == R.id.my_audios) {
            MyAudioFragment fragment = new MyAudioFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame, fragment);
            fragmentTransaction.commit();
    
            getSupportActionBar().setTitle("Мои аудиозаписи");
            //return true;
        } else if (id == R.id.popular_audios) {
            PopularAudioFragment fragment = new PopularAudioFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame, fragment);
            fragmentTransaction.commit();
            getSupportActionBar().setTitle("Поплуярные аудиозаписи");
        } else if (id == R.id.recommend_audios) {
            RecommendationAudioFragment fragment = new RecommendationAudioFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame, fragment);
            fragmentTransaction.commit();
            getSupportActionBar().setTitle("Рекомендуемые аудиозаписи");
       // } else if (id == R.id.clear_cash) {
    
        //} else if (id == R.id.exit_from_profile) {
    
        } else if (id == R.id.item_player) {
            PlayerFragment fragment = new PlayerFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame, fragment);
            fragmentTransaction.commit();
            getSupportActionBar().setTitle("Проигрыватель");
        }
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    

    UPD:

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

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    



  • Add this to the method. onOptionsItemSelected♪ It's the code that handles the pressure on the icon-hamburger and opens/closes the diver:

    if (id == android.R.id.home) 
    {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            drawer.openDrawer(GravityCompat.START);
        }
        return true;
    }
    

    And free advice, don't use the templates while they're asking questions. That is, it is easier to explore how it works by reading separate articles on the diver, fragments etc.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2