簡體   English   中英

導航抽屜點擊不觸發

[英]Navigation Drawer click not triggering

在此處輸入圖像描述如您所見,我在導航抽屜中添加了 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>

您必須在注銷時為大小寫添加括號。

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

點擊其他項目,比如說第二個,是否觸發了這個項目?

如果是,在您的注銷案例中添加一個 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);

我將為您提供我用於我的項目的代碼,並嘗試查看您是否發現任何可以解釋您的代碼不起作用的事實的差異。

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>

這段代碼正在工作(當然不是那樣),所以試着用它做一些交叉檢查,看看你錯過了什么。 當您發現時,很可能很容易忽視和沮喪。 祝你好運!

你能在你的活動的 onCreate 中包含代碼嗎?

一種可能性是您忘記將您的活動注冊為導航視圖的偵聽器。

mNavigationView.setNavigationItemSelectedListener(this)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM