web-dev-qa-db-fra.com

NavigationView OnNavigationItemSelectedListener n'est pas appelé

J'essaie d'utiliser NavigationView à partir de Android Support Library de conception dans mon application. Pour une raison quelconque, OnNavigationItemSelected l'écouteur n'est pas appelé. Voici mon code

Disposition des activités

<?xml version="1.0" encoding="utf-8"?>
<Android.support.v4.widget.DrawerLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    Android:id="@+id/drawer_layout"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:fitsSystemWindows="true">

    <Android.support.design.widget.NavigationView
        Android:id="@+id/navigation_view"
        Android:layout_width="wrap_content"
        Android:layout_height="match_parent"
        Android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu" />

            <FrameLayout
                Android:id="@+id/content_frame"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent" />
</Android.support.v4.widget.DrawerLayout>

Activité surCréer ()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutID());

    toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
    setSupportActionBar(toolbar);
    toolbar.inflateMenu(R.menu.common_menu);
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(final MenuItem menuItem) {
            Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
            menuItem.setChecked(true);
            // allow some time after closing the drawer before performing real navigation
            // so the user can see what is happening
            drawerLayout.closeDrawer(GravityCompat.START);
            mDrawerActionHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    navigate(menuItem.getItemId());
                }
            }, DRAWER_CLOSE_DELAY_MS);
            drawerLayout.closeDrawers();
            return true;
        }

    });
    usernameTextView = (TextView) findViewById(R.id.drawer_header_username);                  
    usernameTextView.setText(getAppDContext().getAccount().getUsername());
   }
16
NinjaCoder

Lorsque vous faites une mise en page XML, vous devez écrire NavigationView après BaseLayout (FrameLayout, LinearLayout, etc.)

<DrawerLayout>
    <FrameLayout />
    <NavigationView />
</DrawerLayout>
65
이종일

Pour moi, cela a fait l'affaire!

navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
9
niva

La disposition principale de votre activité devrait ressembler à ceci:

<Android.support.v4.widget.DrawerLayout
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    Android:id="@+id/navigationDrawer"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

    <include layout="@layout/activity_main_content" />

    <Android.support.design.widget.NavigationView
        Android:id="@+id/navigationView"
        style="@style/NavigationView"
        Android:layout_width="wrap_content"
        Android:layout_height="match_parent"
        Android:layout_gravity="right"
        app:headerLayout="@layout/header"
        app:menu="@menu/menu_drawer"/>

</Android.support.v4.widget.DrawerLayout>

Dans cette NavigationView j'ai lié header.xml et menu_drawer.xml (à partir du dossier menu) par exemple menu_drawer.xml:

<menu xmlns:Android="http://schemas.Android.com/apk/res/Android">
        <group Android:checkableBehavior="single">
            <item
                Android:id="@+id/nav1"
                Android:checked="true"
                Android:icon="@drawable/logo"
                Android:title="Navigation item 1"/>
            <item
                Android:id="@+id/nav2"
                Android:icon="@drawable/logo"
                Android:title="Navigation item 2"/>
        </group>
    </menu>

que votre Java:

public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setUpToolbar();
        setUpNavDrawer();
    }

 private void setUpNavDrawer() {
        NavigationView view = (NavigationView) findViewById(R.id.navigationView);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
        view.setNavigationItemSelectedListener(this);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
    }

Vérifiez si cela fonctionne pour vous. Dans mon projet fonctionne comme un charme.

6
Fares