【问题标题】:Navigation Drawer is not handling click eventsNavigation Drawer 不处理点击事件
【发布时间】:2020-04-10 07:37:17
【问题描述】:

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

问题 2- 不是显示汉堡图标而是显示向后箭头。

我已经尝试搜索但找不到答案。我也尝试添加 .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;
    }

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

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

【问题讨论】:

  • 请不要在场外链接到您的代码。您需要在问题本身中提供minimal reproducible example
  • 您已设置为使用导航组件,但也尝试自己处理导航。如果您打算使用 Navigation 组件,那么您不应该尝试自己处理 NavigationViewFragmentTransactions;该组件将为您处理这些,以及汉堡包/箭头按钮。如果您宁愿以老式的方式进行操作,那么您应该摆脱所有导航的东西,因为这会覆盖您上面的 NavigationViewActionBarDrawerToggle 设置。
  • 我设法通过删除用于手动处理导航的附加代码来修复它,但仍然仅在主页片段上显示返回箭头。

标签: java android xml android-layout navigation-drawer


【解决方案1】:

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

解决方案-实际上是因为我没有在 AppbarConfiguration.Builder

上提供主片段的 ID

之前-

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

之后-

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(drawer)
                .build();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多