当我点击里面的项目时,ONNavigationItemSelectedListener 没有被触发

NavigationItemSelectedListener cannot triggered when i clicked the item inside

我有导航抽屉,上面有项目。但是当我点击时,没有任何反应。这段代码有什么问题?

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 上放置断点时,它不会在断点处停止,所以我无法根据我选择的启动另一个 activity。为什么?

这是我的布局:

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

我的布局有什么问题?

改用此代码

      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;
        }
    });

当您必须使用 switch 语句时,应在每个 case 中使用 break。但是现在你在内部案例块

中使用return

您应该按照此步骤进行操作...

  1. 在你的 BaseOrderActivity 中实现 navigationView linstner 就像 BaseOrderActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener

  2. 在 menu.xml

  3. 中创建菜单
  4. Oncreate

    中初始化 NavigationView
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    
  5. 终于使用这个覆盖方法

      @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;
        }
    

注意:一旦你使用 finish 然后你当前的 Activity/Context 将得到 Stop/Terminate,所以如果你使用安全的方式

希望对你有所帮助..

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

  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 个视图 (child)。第一个 child 是抽屉关闭时显示的,第二个是抽屉打开时显示的。

请参阅下面的示例 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>

改用此代码

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