onNavigationItemSelected 不适用于 DrawerLayout

onNavigationItemSelected not work on DrawerLayout

我尝试将 onNavigationItemSelected 添加到 DrawerLayout 但它不起作用。我可以看到选项,但是当我单击选项时,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;
    }
}

这是完整的 source code

activity_main

<?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"
    tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <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:menu="@menu/setting_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

主要活动

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        val toggle = ActionBarDrawerToggle(
            this,
            drawer_layout,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
        )
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()
        nav_view.setNavigationItemSelectedListener(this)
    }
 override fun onNavigationItemSelected(item: MenuItem): Boolean {
Log.d("abc",item.itemId.toString())
        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }}

尝试创建一个侦听器,然后传递给 setNavigationItemSelectedlistener 而不是 "this"。

喜欢以下内容:

private NavigationView.OnNavigationItemSelectedListener navListener = new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()) {
            case R.id.nav_slideshow:
                appBarTV.setText("Slideshow Page");
                Toast.makeText(this, "Slideshow", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_manage:
                appBarTV.setText("Tools Page");
                Toast.makeText(this, "Tools", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_share:
                appBarTV.setText("Share Page");
                Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_send:
                appBarTV.setText("Send");
                Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();
                break;
        }

        return true;
    }
}

那给你打电话:

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

错误在您的布局中。
您可以查看 docDrawerLayout:

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no <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>