onNavigationItemSelected not getting called

Nick Su picture Nick Su · Apr 13, 2016 · Viewed 11.9k times · Source

I have implemented a navigationview in my app , which was automatically created by AndroidStudio. I picked up the NavigationDrawer Activity when I created a new project and the menu items seem ok , but nothing happend when I click on any of the menu item . Below is my onNavigationItemSelected() method:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    Toast.makeText(MainActivity.this,"onNavigationItemSelected",Toast.LENGTH_LONG).show();
    // Handle navigation view item clicks here.
    item.setChecked(true);
    int id = item.getItemId();

    if (id == R.id.medicalRecord) {
        Toast.makeText(MainActivity.this,id,Toast.LENGTH_LONG).show();
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

And my onCreate method , I create the NavigationView in it

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
    if (babyImage.exists()) {
        Drawable drawable = Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/babycare/temp.jpg");
        linearLayout.setBackground(drawable);
    }

    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.openDrawer(Gravity.LEFT);
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

Of course , I implemented the NavigationView.OnNavigationItemSelectedListener for my class .

I am wondering whether this NavigationView should be implement in Material Design theme or not . Now the theme used in my app is @style/AppTheme

Please , someone help me out , thank you

Answer

Nick Su picture Nick Su · Apr 14, 2016

I solved my question , just simply change the order in my activity_main.xml . I have a LinearLayout viewgroup and NavigationView in my DrawerLayout viewgroup , at first the NavigationView is the first in my viewgroup and now I change the order , the first one is the LinearLayout and the second is NavigationView , and it work as it suppose to be . Dear!!!

But can someone tell me why it happend ? Does it matter the view order in a viewgroup regardless the display sequence .