在android中为NavigationDrawer项设置OnClick

在android中为NavigationDrawer项设置OnClick,android,navigation-drawer,android-architecture-navigation,android-navigationview,android-jetpack-navigation,Android,Navigation Drawer,Android Architecture Navigation,Android Navigationview,Android Jetpack Navigation,我做了一个导航抽屉,里面有20-25件物品。它的路径是res/menu/navigation\u menu.xml。在我的主要活动中,我编写了以下代码,据我目前所知,这是导航抽屉的默认代码: findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

我做了一个导航抽屉,里面有20-25件物品。它的路径是res/menu/navigation\u menu.xml。在我的主要活动中,我编写了以下代码,据我目前所知,这是导航抽屉的默认代码:

findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

        NavigationView navigationView = findViewById(R.id.navigationView);
        navigationView.setItemIconTintList(null);

        NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
        NavigationUI.setupWithNavController(navigationView, navController);

        navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
                tv1.setText(destination.getLabel());
            }
        });
这里的“R.id.imageMenu”是打开抽屉的3个水平线图像。 这是我的activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="#0097A7">

            <ImageView
                android:id="@+id/imageMenu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="20dp"
                android:src="@drawable/ic_menu_white_24dp">
            </ImageView>

            <TextView
                android:id="@+id/mainTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="All docs"
                android:textSize="18sp"
                android:layout_centerVertical="true"
                android:layout_marginStart="55dp"
                android:textColor="#FFFFFF">
            </TextView>

            <ImageButton
                android:id="@+id/mbib"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="20dp"
                android:background="@drawable/ic_more_vert_white_24dp">
            </ImageButton>

        </RelativeLayout>

        <fragment
            android:id="@+id/navHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="55dp"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main">
        </fragment>

        <androidx.cardview.widget.CardView
            android:id="@+id/cv1"
            android:visibility="invisible"
            android:layout_width="250dp"
            android:layout_height="130dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="18dp"
            android:layout_marginRight="25dp"
            app:cardCornerRadius="7.5dp"
            android:background="#47433F">

            <RelativeLayout
                android:id="@+id/mbrl1"
                android:layout_width="match_parent"
                android:background="?android:attr/selectableItemBackground"
                android:layout_height="65dp">

                <TextView
                    android:id="@+id/mbtv1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp"
                    android:layout_marginLeft="28dp"
                    android:drawableLeft="@drawable/ic_image_white_24dp"
                    android:textColor="#FFFFFF"
                    android:textSize="15sp"
                    android:text="      Import from Gallery">
                </TextView>

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/mbrl2"
                android:layout_width="match_parent"
                android:background="?android:attr/selectableItemBackground"
                android:layout_height="65dp"
                android:layout_marginTop="65dp">

                <TextView
                    android:id="@+id/mbtv2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="28dp"
                    android:drawableLeft="@drawable/ic_picture_as_pdf_white_24dp"
                    android:textColor="#FFFFFF"
                    android:textSize="15sp"
                    android:text="      Import PDF">
                </TextView>

            </RelativeLayout>

        </androidx.cardview.widget.CardView>

    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        app:headerLayout="@layout/layout_navigation_header"
        android:layout_gravity="start">
    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

在我的导航抽屉项目中,我有两个项目名为Rate UsShare,我没有为它们定义任何片段,因为我想为它们做一些其他工作(比如Rate Us,我将在playstore中打开我的应用程序链接)

我的问题: 我希望我的导航抽屉能够像现在一样工作,除非单击了为我们评分共享项目。在这种情况下(它没有做任何事情,因为它们没有碎片),我想在playstore中打开我的应用程序链接或其他一些工作。我怎样才能做到这一点?多谢各位


如果我缺少代码的任何部分,请告诉我,我将更新我的问题。

好的比率共享

   @Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.rateus) {
     Intent rateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())); 
      startActivity(rateIntent);


    } else if (id == R.id.share) {
     //just an example
     String message = "Text I want to share.";
     Intent share = new Intent(Intent.ACTION_SEND);
     share.setType("text/plain");
    share.putExtra(Intent.EXTRA_TEXT, message);

    startActivity(Intent.createChooser(share, "Title of the dialog the system will open"));


    } else if (id == R.id.anything) {

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

您可以使用自定义的
NavigationItemSelectedListener
,但通过调用
setNavigationItemSelectedListener
,您将删除原始侦听器

在这种情况下,您必须通过调用
NavigationUI.onNavDestinationSelected()
触发默认行为

比如:

navigationView.setNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.xxxxx -> {
                    // handle click
                    true
                }
            }

             NavigationUI.onNavDestinationSelected(it, navController)
             drawerLayout.closeDrawer(GravityCompat.START)
             true
        }

代码就像一个符咒@Gabriele Mariotti。非常感谢你的帮助。向上投票,我接受你的回答。我感谢你@Wini的帮助,但对于你的代码,我必须为我导航抽屉中的所有20-25项写if-else。但我只是想写下if-else为我们和大家分享的价格。对于所有其他项目,我希望使用默认行为。加布里埃尔·马里奥蒂的回答正好解释了我需要什么。我也一定会投票支持你的答案,因为我可以使用你给出的代码部分来分享和评分。塔克斯