导航抽屉点击不触发

时间:2018-03-07 12:12:07

标签: android navigation-drawer

enter image description here正如您所看到的,我在导航抽屉中添加了6个项目,但在单击“注销”时未触发。抱歉代码格式不好。我是新来的。

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_log_out) {
        return true;
    }

    return super.onOptionsItemSelected(item); 
}

@SuppressWarnings("StatementWithEmptyBody") 
@Override public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();
    switch (id){
        case R.id.nav_profile:
            break;
        case R.id.nav_wallet:
            break;
        case R.id.nav_search:
            break;
        case R.id.nav_about_us:
            break;
        case R.id.nav_contact_us:
            break;
        case R.id.nav_log_out:
            Intent intent = new Intent(this,LogIn.class);
            FirebaseAuth.getInstance().signOut();
            startActivity(intent);
}

这是我的菜单项XML文件。 id与我在XML文件中声明的相同。注销操作栏工作正常,但它在导航抽屉中不起作用。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_profile"
            android:icon="@drawable/ic_person_black_24dp"
            android:title="Profile" />
        <item
            android:id="@+id/nav_wallet"
            android:icon="@drawable/ic_attach_money_black_24dp"
            android:title="EasyFare Wallet" />
        <item
            android:id="@+id/nav_search"
            android:icon="@drawable/ic_directions_bus_black_24dp"
            android:title="Search Bus" />
        <item
            android:id="@+id/nav_about_us"
            android:icon="@drawable/ic_folder_shared_black_24dp"
            android:title="About Us"/>
        <item
            android:id="@+id/nav_contact_us"
            android:icon="@drawable/ic_email_black_24dp"
            android:title="Contact Us" />
        <item
            android:id="@+id/nav_log_out"
            android:icon="@drawable/ic_exit_to_app_black_24dp"
            android:title="Log Out" />
    </group>

</menu>
带有DrawerLayout的

活动文件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

4 个答案:

答案 0 :(得分:1)

您必须在退出时为案例添加括号..

case R.id.nav_log_out:
{
        Intent intent = new Intent(this,LogIn.class);
        FirebaseAuth.getInstance().signOut();
        startActivity(intent);
}

答案 1 :(得分:0)

点击其他项目,让我们说第二项,触发此项目?

如果是,请在注销案例中添加break语句,并创建默认案例。

您也可以尝试使用以下示例替换代码。

@SuppressWarnings("StatementWithEmptyBody") 
@Override public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();
    switch (id){
        case R.id.nav_profile:
            break;
        case R.id.nav_wallet:
            break;
        case R.id.nav_search:
            break;
        case R.id.nav_about_us:
            break;
        case R.id.nav_contact_us:
            break;
        case R.id.nav_log_out:
            {
            Intent intent = new Intent(this,LogIn.class);
            FirebaseAuth.getInstance().signOut();
            startActivity(intent);
            break;
            }
        DrawerLayout drawer;
        drawer = findViewById(R.id.THE_ID_OF_YOUR_DRAWER);  
        drawer.closeDrawer(GravityCompat.START);
        return true;
   }

}

另请确保在OnCreateOnStartonResume中包含以下内容

 DrawerLayout drawer;

 drawer = findViewById(R.id.THE_ID_OF_YOUR_DRAWER);

答案 2 :(得分:0)

我将为您提供我用于我的项目的代码,并尝试查看是否发现任何可以解释您的代码不起作用的差异。

AnnouncementsActivity.java

    .
    .
    .
    .



public class AnnouncementsActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {






    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_announcements);
            .
            .
            .
            .



                Toolbar toolbar = findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);



                DrawerLayout drawer;

                drawer = findViewById(R.id.drawer_layout_astiko);



                ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);
                toggle.syncState();

                toolbar.setNavigationIcon(R.mipmap.ic_toolbar);



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

           .
           .
           .
           .



           }






        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            int id = item.getItemId();

            if (id == R.id.nav_first_item) {

                if(prev_activity_routes){
                    Intent intent = new Intent(this,RoutesActivity.class);
                    intent.putExtra("Direction Forth",bus_direction_forth);
                    intent.putExtra("Team",team);
                    intent.putExtra("Route name", value1);
                    intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
                    startActivity(intent);
                }else{

                    if(isAstiko){
                        Intent intent = new Intent(this,TicketIssuanceActivityAstiko.class);
                        intent.putExtra("Direction Forth",bus_direction_forth);
                        intent.putExtra("Team",team);
                        intent.putExtra("Route name", value1);
                        intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
                        startActivity(intent);
                    }else{
                        Intent intent = new Intent(this,TicketIssuanceActivity.class);
                        intent.putExtra("Direction Forth",bus_direction_forth);
                        intent.putExtra("Team",team);
                        intent.putExtra("Route name", value1);
                        intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
                        startActivity(intent);
                    }

                }

            } else if (id == R.id.nav_second_item) {
                Intent intent = new Intent(this,DefinedTicketsActivity.class);
                intent.putExtra("Direction Forth",bus_direction_forth);
                intent.putExtra("Team",team);
                intent.putExtra("Route name", value1);
                intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
                intent.putExtra("PreviousActivityRoutes",prev_activity_routes);
                intent.putExtra("isAstiko",isAstiko);
                startActivity(intent);
            }

            .
            .
            .
            .


            }else if (id == R.id.nav_eighth_item) {
                Intent intent = new Intent(this, SignInActivity.class);
                startActivity(intent);
            }

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





}

ActivityAnnouncements.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_announcements"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="500dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:theme="@style/NavigationDrawerStyle"
        app:headerLayout="@layout/my_nav_header"
        app:menu="@menu/my_drawer" />

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

app_bar_announcements.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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="com.e_analysis.AlphaPro.MobileTicketing.AnnouncementsActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/MyTheme">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/MyTheme"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_announcements" />


</android.support.design.widget.CoordinatorLayout>

content_announcements.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:layout_marginBottom="-0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    android:gravity="fill"
    tools:context="com.e_analysis.AlphaPro.MobileTicketing.AnnouncementsActivity"
    tools:showIn="@layout/app_bar_announcements">


    .
    .
    .
    .



</LinearLayout>
菜单子文件夹中的

my_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_first_item"
                android:icon="@mipmap/ic_printer_black"
                android:title="@string/nav_first_item_value"/>
            <item
                android:id="@+id/nav_second_item"
                android:icon="@mipmap/ic_tune_vertical"
                android:title="@string/nav_second_item_value" />

            .
            .
            .
            .

            <item
                android:id="@+id/nav_eighth_item"
                android:icon="@mipmap/ic_logout"
                android:title="@string/nav_eighth_item_value" />
        </group>


</menu>

my_nav_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

    .
    .
    .
    .

</LinearLayout>

此代码正在运行(不是当前的代码)所以尝试与它进行一些交叉检查,看看你缺少什么。当你发现它时,很可能会容易被忽视和挫败。祝你好运!

答案 3 :(得分:0)

您可以在活动的onCreate中添加代码吗?

一种可能性是您忘记将您的活动注册为导航视图的监听器。

mNavigationView.setNavigationItemSelectedListener(this)