导航抽屉不处理点击事件

问题描述 投票:-1回答:1

问题1-我的导航抽屉没有处理点击事件。

问题2-我的导航抽屉没有显示汉堡包图标,而是显示反向箭头。

我试着搜索了一下,但没有找到答案,我也试着添加.bringToFront()方法,但还是不行。我也试过添加.bringToFront()方法,但还是不行。

MainActivity.java:-

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        TextView navUserName = findViewById(R.id.textViewNav_username);
        TextView navUserEmail = findViewById(R.id.textViewNav_useremail);
        mAuth = FirebaseAuth.getInstance();
        currentUser = mAuth.getCurrentUser();
        DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference volunteerRef = dbRef.child("Volunteer").child(currentUser.getUid());
        //If current user is null go to login activity
        if (currentUser == null) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }


        //Creates hamburger animated icon
        drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle actionBarDrawerToggle= new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawer.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        //Sets click listner to navigation item 1/2
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.bringToFront();
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.nav_option_home);

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

    }

    //Sets click listner to navigation item 2/2
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Log.d("main1122", "inside listner");
        switch (item.getItemId()){
            case R.id.nav_option_home:
                Log.d("main1122", "Clicked item" + item.getItemId());
                getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
                        new HomeFragment()).commit();
                break;
            case R.id.nav_option_profile:
                Log.d("main1122", "Clicked item" + item.getItemId());
                getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
                        new profile()).commit();
                break;
            case R.id.nav_option_contact:
                getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
                        new contact()).commit();
                break;
            case R.id.nav_option_about:
                getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
                        new AboutFragment()).commit();
                break;
            case R.id.nav_option_share:
                //TODO: Share app action
                Toast.makeText(this,"Share clicked",Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_option_logout:
                //TODO: Logout action
                break;
        }

        drawer.closeDrawer(GravityCompat.START);
        //Return false will make menu item unselected(not highlighted)
        return true;
    }

活动_main.xml (包含R.id.nav_view) :-。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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_nav__drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.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_nav__drawer"
        app:menu="@menu/activity_nav__drawer_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

移动导航.xml 包含我想在点击菜单项后启动的片段):-。

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.helpinghandsorg.helpinghands.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home"/>

    <fragment
        android:id="@+id/nav_about"
        android:name="com.helpinghandsorg.helpinghands.AboutFragment"
        android:label="@string/menu_about"
        tools:layout="@layout/fragment_about">
        <action
            android:id="@+id/action_nav_about_to_nav_home"
            app:destination="@id/nav_home" />
    </fragment>

    <fragment
        android:id="@+id/nav_contact"
        android:name="com.helpinghandsorg.helpinghands.ui.contact.contact"
        android:label="@string/menu_contact"
        tools:layout="@layout/contact_fragment">
        <action
            android:id="@+id/action_nav_contact_to_nav_home"
            app:destination="@id/nav_home" />
    </fragment>

    <fragment
        android:id="@+id/nav_profile"
        android:name="com.helpinghandsorg.helpinghands.ui.profile.profile"
        android:label="@string/menu_profile"
        tools:layout="@layout/profile_fragment">
        <action
            android:id="@+id/action_nav_profile_to_nav_home"
            app:destination="@id/nav_home" />
    </fragment>
</navigation>

activity_nav_drawer_drawer.xml)。 (包含菜单项ID):-。

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

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_option_home"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/nav_option_profile"
            android:icon="@drawable/ic_account_circle_black_24dp"
            android:title="@string/menu_profile" />
        <item
            android:id="@+id/nav_option_contact"
            android:icon="@drawable/ic_email_black_24dp"
            android:title="@string/menu_contact" />
        <item
            android:id="@+id/nav_option_about"
            android:icon="@drawable/ic_info_black_24dp"
            android:title="@string/menu_about" />
        <item
            android:id="@+id/nav_option_share"
            android:icon="@drawable/ic_menu_share"
            android:title="@string/menu_share" />
        <item
            android:id="@+id/nav_option_logout"
            android:icon="@drawable/ic_exit_to_app_black_24dp"
            android:title="@string/menu_logout" />
    </group>
</menu>
java android xml android-layout navigation-drawer
1个回答
0
投票

回答我的第2个问题.问题-不是显示汉堡包图标,而是显示向后的箭头。

解決方法-其實是因為我沒有在AppbarConfiguration.Builder中提供home片段的ID。

Before- mAppBarConfiguration = new AppBarConfiguration.Builder( R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout) .setDrawerLayout(抽屉) .build();

After- mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home, R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout) .setDrawerLayout(抽屉) .build();

© www.soinside.com 2019 - 2024. All rights reserved.