2017-09-22 48 views
0

我有导航抽屉,它上面有物品。但是当我点击时,没有任何反应。这段代码有什么问题?当我点击里面的物品时,NavigationItemSelectedListener无法触发

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 
       switch (menuItem.getItemId()) { 
        case R.id.navi_1: 
         Intent intent = new Intent(BaseOrderActivity.this,BaseOrderActivity.class); 
         intent.putExtra("dataDealerCode",dataorderCode); 
         startActivity(intent); 
         finish(); 
         return true; 
        case R.id.navi_2: 
         Intent intent2 = new Intent(BaseOrderActivity.this,MappingProductRecycler.class); 
         intent2.putExtra("dataDealerCode",dataorderCode); 
         startActivity(intent2); 
         finish(); 
         return true; 
        case R.id.navi_3: 
         Intent intent3 = new Intent(BaseOrderActivity.this,UpdateProductActivity.class); 
         intent3.putExtra("dataDealerCode",dataorderCode); 
         startActivity(intent3); 
         finish(); 
         return true; 
        case R.id.navi_4: 
         return true; 
       } 
       //drawerLayout.closeDrawers(); 
       return true; 
      } 
     }); 

当我把断点NavigationItemSelected,它不会停止在断点,所以我不能启动我的选择对其他活动的基础。为什么?

这是我的布局:

<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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:fitsSystemWindows="true" 
    android:layout_height="match_parent"> 
    <FrameLayout 
     android:animateLayoutChanges="true" 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".BaseOrderActivity" 
     /> 
    <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:menu="@menu/navigation"/> 

<RelativeLayout 
    android:id="@+id/main_layout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".BaseOrderActivity"> 


    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tab_layout" 
     android:layout_y="50dp" /> 

</RelativeLayout> 


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

什么错我的布局?

+0

尝试把你的onNavigationItemSelected,DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout)结束; drawer.closeDrawer(GravityCompat.START); invalidateOptionsMenu(); 返回true; – zephyr

+0

你能告诉我们你的XML为导航抽屉和菜单项吗? –

+0

@zephyr我已经把这段代码放到了我的onNavigationSelected结尾,但它没有任何改变。 –

回答

0

既然你有了,您的导航项目选择似乎不起作用,以下是可能的故障排除步骤:

  1. 检查导航视图是否已正确初始化。

    mNavigationView = (NavigationView) findViewById(R.id.navigation); //navigation is the id of the xml for your navigation view. 
    
  2. 入住交换机情况下的ID。 R.id.navi_1:等(navi_1, etc.)确实有效。

编辑:

你应该有2只在你的抽屉布局视图(孩子)。第一个孩子是抽屉关闭时显示的孩子,第二个孩子是抽屉打开时显示的孩子。

请参阅下面的示例xml。

<?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"> 

    // this is the main screen, shown when the drawer is CLOSED 
    // 1st VIEW 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <include 
      layout="@layout/toolbar"/> 

     <ListView 
      android:id="@+id/list_view_home_users" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 


    </LinearLayout> 

    // This view is shown when drawer is OPEN 
    // 2nd View 
    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/nav_header_layout" 
     app:itemTextAppearance="@style/TextAppearance.Design.Snackbar.Message" 
     app:menu="@menu/my_navigation_items" /> 
</android.support.v4.widget.DrawerLayout> 

编辑2: 在你的情况,你可以相对布局的第一个视图。

<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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:fitsSystemWindows="true" 
    android:layout_height="match_parent"> 
    <RelativeLayout 
     android:id="@+id/main_layout" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".BaseOrderActivity"> 


     <android.support.design.widget.TabLayout 
      android:id="@+id/tab_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:elevation="6dp" 
      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 


     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/tab_layout" 
      android:layout_y="50dp" /> 

    </RelativeLayout> 
    <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:menu="@menu/navigation"/> 

</android.support.v4.widget.DrawerLayout> 
+0

thankss,这个布局是这个问题。 –

0

当你不得不使用switch语句应该在你的每一个case使用break试试这个代码,而不是

 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     Intent intent; 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 
      switch (menuItem.getItemId()) { 
       case R.id.navi_1: 
        intent= new Intent(BaseOrderActivity.this,BaseOrderActivity.class); 
        intent.putExtra("dataDealerCode",dataorderCode); 
        startActivity(intent); 
        finish(); 
        break; 
       case R.id.navi_2: 
        intent = new Intent(BaseOrderActivity.this,MappingProductRecycler.class); 
        intent.putExtra("dataDealerCode",dataorderCode); 
        startActivity(intent); 
        finish(); 
        break; 
       case R.id.navi_3: 
        intent = new Intent(BaseOrderActivity.this,UpdateProductActivity.class); 
        intent.putExtra("dataDealerCode",dataorderCode); 
        startActivity(intent); 
        finish(); 
        break; 
       case R.id.navi_4: 
        break; 
      } 
      //drawerLayout.closeDrawers(); 
      return true; 
     } 
    }); 
+0

你能解释一下这是怎么解决这个问题的?无论如何,开关盒会断开,并返回“真”。 –

+0

然后问题是不是与开关案件right.is它会在案件内? – Anonymous

+0

从句法上来看,你的代码和OP的代码段没有区别。 –

0

。但是现在您使用内case块回报

你应该遵循这个步骤,将工作...

  1. 落实navigationView linstner在BaseOrderActivityBaseOrderActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener

  2. 创建菜单在你的menu.xml中

  3. 初始化你NavigationView在Oncreate

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
    
  4. 最后用这个替代方法

    @Override 
        public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.main_activity, menu); 
         return true; 
        } 
    
        @SuppressWarnings("StatementWithEmptyBody") 
        @Override 
        public boolean onNavigationItemSelected(MenuItem item) { 
         // Handle navigation view item clicks here. 
         int id = item.getItemId(); 
         Intent intent = null; 
         switch (id) { 
          case R.id.navi_1: 
           intent = new Intent(this,BaseOrderActivity.class); 
           intent.putExtra("dataDealerCode",dataorderCode); 
           startActivity(intent); 
           break; 
          case R.id.navi_2: 
           intent = new Intent(this,MappingProductRecycler.class); 
           intent.putExtra("dataDealerCode",dataorderCode); 
           startActivity(intent); 
           break; 
         } 
         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
         drawer.closeDrawer(GravityCompat.START); 
         return true; 
        } 
    

注意:一旦使用结束那么你的当前活动/上下文将越来越停止/终止,因此,如果您使用安全的方式

希望它能帮到你..

+0

它不是关于“break”,即使我把断点和调试onNavigationItemSelected,调试不能在那里,就像我的选择没有触发 –

+0

谢谢,我更新了我的布局,也许这种布局导致这个问题@Raja –

0

试试这个代码,而不是

int ids=menuItem.getItemId(); 
switch (ids) { 
    case R.id.itemID: 
      //Your Code 
     break 
    default: 
     break; 
} 
drawerLayout.closeDrawers(); 
return true; 
相关问题