首页 > 解决方案 > onNavigationItemSelected 不适用于 DrawerLayout

问题描述

我尝试添加onNavigationItemSelectedDrawerLayout但它不起作用。我可以看到选项,但是当我单击选项时,它DrawerLayout正在关闭并且什么也没发生。

我不知道是什么问题,很长一段时间,我都在寻找解决方案,但找不到。

xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
  >

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <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_main"
            app:menu="@menu/activity_main_drawer" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

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

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tablayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                app:tabSelectedTextColor="@android:color/white"
                app:tabTextColor="@android:color/black">

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tabChats"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Table1" />

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tabStatus"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Table2" />

            </com.google.android.material.tabs.TabLayout>

            <androidx.viewpager.widget.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


        </LinearLayout>


    </androidx.drawerlayout.widget.DrawerLayout>

</RelativeLayout>

和java代码


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawer;
    private TextView appBarTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        appBarTV = findViewById(R.id.appbar_text_view);

        ImageButton menuRight = findViewById(R.id.leftRight);
        menuRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    drawer.openDrawer(GravityCompat.START);
                }
            }
        });

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



    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        drawer.closeDrawers();

        int id = item.getItemId();


 if (id == R.id.nav_slideshow) {
            appBarTV.setText("Slideshow Page");
            Toast.makeText(this, "Slideshow", Toast.LENGTH_SHORT).show();

        } else if (id == R.id.nav_manage) {
            appBarTV.setText("Tools Page");
            Toast.makeText(this, "Tools", Toast.LENGTH_SHORT).show();

        } else if (id == R.id.nav_share) {
            appBarTV.setText("Share Page");
            Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();

        } else if (id == R.id.nav_send) {
            appBarTV.setText("Send");
            Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();
        }

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

这是完整的源代码

标签: javaandroidnavigation-drawernavigationviewandroid-navigationview

解决方案


错误在您的布局中。
您可以查看以下文档DrawerLayout

要使用 a DrawerLayout,请将您的主要内容视图定位为第一个子视图,其宽度和高度为 match_parent 并且没有<layout_gravity>

改变你的布局:

 <androidx.drawerlayout.widget.DrawerLayout
       ..... 
       tools:openDrawer="start">

        <!-- NavigationView -->
        <com.google.android.material.navigation.NavigationView
            ../>

        <!-- main content view -->
        <LinearLayout...>

        </LinearLayout>

    </androidx.drawerlayout.widget.DrawerLayout>

至:

 <androidx.drawerlayout.widget.DrawerLayout
       ..... 
       tools:openDrawer="start">

        <!-- main content view -->
        <LinearLayout...>

        </LinearLayout>

        <!-- NavigationView -->
        <com.google.android.material.navigation.NavigationView
            ../>


    </androidx.drawerlayout.widget.DrawerLayout>

推荐阅读