2015-10-16 76 views
0

我在android.I中創建了一個導航視圖,它在菜單文件夾下的draweritems下添加了項目。每次點擊menuitem時,都會出現一個toast,但現在它沒有響應。下面是代碼。請幫我...NavigationView Unresponsive

draweritems.xml:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/motel" 
      android:title="Motel" 
      android:icon="@drawable/motel" 
      > 

     </item> 
     <item 
      android:id="@+id/packages" 
      android:title="Packages" 
      android:icon="@drawable/packages"> 
     </item> 
     <item 
      android:id="@+id/ayurveda" 
      android:title="Ayurveda" 
      android:icon="@drawable/ayurveda"> 
     </item> 
     <item 
      android:id="@+id/marketing" 
      android:title="Marketing" 
      android:icon="@drawable/marketing"> 
     </item> 
     <item 
      android:id="@+id/Tours" 
      android:title="ConductedTours" 
      android:icon="@drawable/tours"> 
     </item> 
     <item 
      android:id="@+id/Locate" 
      android:title="Locate" 
      android:icon="@drawable/locate"> 
     </item> 
     <item 
      android:id="@+id/News" 
      android:title="News" 
      android:icon="@drawable/news"> 
     </item> 
     <item 
      android:id="@+id/Login" 
      android:title="Login" 
      android:icon="@drawable/login"> 
     </item> 
    </group> 

</menu> 

MainActivity.java:

import android.content.Context; 
    import android.os.Bundle; 
    import android.support.design.widget.NavigationView; 
    import android.support.v4.widget.DrawerLayout; 
    import android.support.v7.app.ActionBarDrawerToggle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.util.Log; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.Toast; 

    public class MainActivity extends AppCompatActivity { 
     private Toolbar toolbar; 
     private Context context=this; 
     private NavigationView navigationView; 
     private DrawerLayout drawerLayout; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
        Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
      navigationView=(NavigationView) findViewById(R.id.navigation_view); 
      navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(MenuItem menuItem) { 
        Log.i("Clicked", "Clicked motel"); 
        if (menuItem.isChecked()) { 
         menuItem.setChecked(false); 
        } else menuItem.setChecked(true); 

        drawerLayout.closeDrawers(); 
        switch (menuItem.getItemId()) { 
         case R.id.motel: 

          Toast.makeText(getApplicationContext(), "Motels", Toast.LENGTH_LONG).show(); 

          return true; 
         case R.id.packages: 
          Toast.makeText(getApplicationContext(), "Packages", Toast.LENGTH_LONG).show(); 
          return true; 
         case R.id.ayurveda: 
          Toast.makeText(getApplicationContext(), "Ayurveda", Toast.LENGTH_LONG).show(); 
          return true; 


        } 
        return true; 
       } 
      }); 
      drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
      ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){ 



       @Override 
       public void onDrawerClosed(View drawerView) { 
        // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank 
        super.onDrawerClosed(drawerView); 
       } 

       @Override 
       public void onDrawerOpened(View drawerView) { 
        // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank 

        super.onDrawerOpened(drawerView); 
       } 
      }; 

      //Setting the actionbarToggle to drawer layout 
      drawerLayout.setDrawerListener(actionBarDrawerToggle); 

      //calling sync state is necessay or else your hamburger icon wont show up 
      actionBarDrawerToggle.syncState(); 



     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.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(); 
      if(id==R.id.ayurveda){ 
       Toast.makeText(getApplicationContext(),"Clicked Ayurveda",Toast.LENGTH_LONG).show(); 
       return true; 
      } 
      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 


    } 

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/drawerLayout" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/header" 
     app:menu="@menu/draweritems" 
     app:theme="@style/NavigationViewStyle"> 

    </android.support.design.widget.NavigationView> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/relative" 
     android:orientation="vertical"> 
     <include 
      android:id="@+id/toolbar" 
      layout="@layout/toolbar"/> 

    </RelativeLayout> 
</android.support.v4.widget.DrawerLayout> 

styles.xml:

<!-- Base application theme. --> 

    <!-- Customize your theme here. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

    <item name="windowActionBar">false</item> 
    <item name="windowActionModeOverlay">true</item> 
    <item name="android:windowDrawsSystemBarBackgrounds">true</item> 

</style> 
<style name="NavigationViewStyle"> 
    <item name="android:textSize">20sp</item> 

    <!-- menu item text size--> 
    <item name="android:listPreferredItemHeightSmall">80dp</item><!-- menu item height--> 
</style> 

+0

post navigationview xml文件 –

+0

發佈導航視圖 – jobin

+0

您必須設置選擇器以標識所選項目 –

回答

1

此代碼更改

public class MainActivity extends AppCompatActivity 

以下代碼

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener 

我不確定,但我希望你能做到。