gpt4 book ai didi

android - 在android中设置NavigationDrawer项目的OnClick

转载 作者:行者123 更新时间:2023-12-05 00:19:29 25 4
gpt4 key购买 nike

我制作了一个抽屉导航,其中包含 20-25 个项目。它的路径是res/menu/navigation_menu.xml。在我的主要 Activity 中,我编写了以下代码,据我所知,到目前为止,这是抽屉导航的默认代码:

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 个名为 给我们评分分享 的项目,我没有为其定义任何 fragment ,因为我想为它们做一些其他工作(就像给我们评分一样,我将在 Playstore 中打开我的应用程序链接)。

我的问题:我希望我的抽屉导航应该像现在一样工作,除非点击评价我们分享项目。在这种情况下(它没有做任何事情,因为没有 fragment )我想在 Playstore 或其他一些工作中打开我的应用程序链接。我怎样才能实现这个目标?谢谢。

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

最佳答案

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

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

类似于:

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

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

关于android - 在android中设置NavigationDrawer项目的OnClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62773966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com