简体   繁体   English

汉堡图标未打开导航抽屉

[英]Hamburger icon not opening navigation drawer

When i open my app and click on the hamburger icon, it is does not open the navigation drawer.当我打开我的应用程序并单击汉堡包图标时,它不会打开导航抽屉。 But when i swipe from the edges it is opening.但是当我从边缘滑动时,它正在打开。 Also the drawer responds to hamburger icon click events after the swipe action.此外,抽屉在滑动操作后响应汉堡图标点击事件。

I tried every solution on stackoverflow but couldn't resolve the issue.我在stackoverflow上尝试了所有解决方案,但无法解决问题。 Please someone help.请有人帮忙。 Thanks in advance.提前致谢。

XML Code: XML 代码:

<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start"
tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:title="Nexzen"
                app:titleTextColor="@color/white"
                android:background="@color/dark_green"/>

    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"
        android:background="@color/dark_green"
        app:tabTextColor="@color/white"
        app:tabIndicator="@color/receive_message"
        app:tabSelectedTextColor="@color/receive_message"
        android:layout_below="@id/appBarLayout"/>

    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"
        android:layout_below="@id/tabLayout"/>

</RelativeLayout>

<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/navmenu"
    android:visibility="gone"
    app:menu="@menu/drawer_menu"
    app:itemTextColor="@color/text_color"
    app:itemIconTint="@color/text_color"
    app:headerLayout="@layout/nav_header"
    android:layout_gravity = "start" />

</androidx.drawerlayout.widget.DrawerLayout>

Activity code:活动代码:

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding = ActivityMainBinding.inflate(getLayoutInflater());
            setContentView(binding.getRoot());
    
            setSupportActionBar(binding.toolbar);
    
            toggle = new ActionBarDrawerToggle(this, binding.drawer, binding.toolbar, R.string.open, R.string.close);
            toggle.getDrawerArrowDrawable().setColor(getColor(R.color.white));
            toggle.setDrawerIndicatorEnabled(true);
            toggle.setDrawerSlideAnimationEnabled(true);
            binding.drawer.addDrawerListener(toggle);
            toggle.syncState();
    
        }
    
    @Override
        protected void onStart() {
            super.onStart();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            return super.onCreateOptionsMenu(menu);
        }
    
        @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            if (toggle.onOptionsItemSelected(item)) {
                binding.drawer.openDrawer(GravityCompat.START);
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        protected void onPostCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            toggle.syncState();
        }
    
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            finish();
        }

Ensure you haven't set the visibility of the NavigationView in the fragment to invisible or gone.确保您没有将片段中 NavigationView 的可见性设置为不可见或消失。 This was the cause for me.这就是我的原因。

Swiping must make the navigation drawer visible.滑动必须使导航抽屉可见。 Clicking the hamburger button does not.单击汉堡包按钮不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM