【问题标题】:Setting OnClick for NavigationDrawer Items in android在android中为导航抽屉项目设置OnClick
【发布时间】:2020-07-07 11:11:48
【问题描述】:

我制作了一个导航抽屉,里面有大约 20-25 个项目。它的路径是 res/menu/navigation_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>

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

我的问题: 我希望我的导航抽屉应该像现在一样正常工作,除非单击 Rate UsShare 项目。在那种情况下(它没有做任何事情,因为它们没有片段)我想在 Playstore 或其他一些工作中打开我的应用程序链接。我怎样才能做到这一点?谢谢。

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

【问题讨论】:

    标签: android navigation-drawer android-architecture-navigation android-navigationview android-jetpack-navigation


    【解决方案1】:

    您可以使用自定义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 的魅力。非常感谢您的帮助。一个赞成票,我接受你的回答。
    【解决方案2】:

    评分 && 分享

    没问题
       @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;
    }
    

    【讨论】:

    • 感谢您的帮助@Wini,但对于您的代码,我必须为导航抽屉中的所有 20-25 个项目编写 if else。但我只是想写 if else 来评价我们并分享。对于所有其他项目,我想要默认行为。 Gabriele Mariotti 的回答正是我所需要的。为了您的帮助,我肯定也会支持您的答案,因为我可以使用您提供的代码部分进行分享和评分。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多