当我点击里面的项目时,无法触发NavigationItemSelectedListener

时间:2017-09-22 05:27:27

标签: android navigation-drawer drawerlayout

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

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>

我的布局有什么问题?

4 个答案:

答案 0 :(得分:0)

尝试使用此代码

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

答案 1 :(得分:0)

如果必须使用switch语句,则应在break case中使用BaseOrderActivity。但现在您在内部案例块中使用返回

你应该按照这个步骤运作......

  1. 在您的BaseOrderActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener中实施navigationView linstner,例如Oncreate

  2. 在menu.xml中创建菜单

  3. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);

    中初始化NavigationView
      @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;
        }
    
  4. 最后使用此覆盖方法

    AudioRequest
  5. 注意:使用完成后,您当前的活动/上下文将停止/终止,所以如果您使用安全方式

    希望它会帮助你..

答案 2 :(得分: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.)确实有效。

  3. 编辑:

    您的抽屉布局中应该只有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>
    

答案 3 :(得分:0)

尝试使用此代码

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