【发布时间】:2026-02-02 19:30:01
【问题描述】:
我设置了一个导航抽屉,首先它在 ocCreate 方法中显示一个谷歌地图。我想按导航抽屉菜单中的另一个按钮并更改地图片段。
我有一个带有 app_bar_nav.xml 的 activity_nav.xml,其中包含 content_nav.xml。
我关注了 google maps api 网站,以便在 google 开发人员中实现地图和导航抽屉部分。
现在我被困在这里,因为我不知道如何切换到同一个导航抽屉内的另一个片段。
我的 content_nav.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".NavActivity"
tools:showIn="@layout/app_bar_nav">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavActivity" />
</android.support.constraint.ConstraintLayout>
我的 onCreate() 方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav);
mAuth = FirebaseAuth.getInstance();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_home);
/*Update the UI with user info*/
FirebaseUser user = mAuth.getCurrentUser();
TextView email = navigationView.getHeaderView(0).findViewById(R.id.navEmail);
email.setText(user.getEmail());
TextView name = navigationView.getHeaderView(0).findViewById(R.id.navUsername);
name.setText(user.getDisplayName());
//Setup Map
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
切换功能:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the home action
} else if (id == R.id.nav_profile) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_tools) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_logout) {
mAuth.signOut();
SharedPreferences sp = getSharedPreferences("Login", MODE_PRIVATE);
SharedPreferences.Editor Ed = sp.edit();
Ed.putString("Email", null);
Ed.putString("Password", null);
Ed.apply();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
【问题讨论】:
-
你在哪里设置你的项目的 OnClickListener ?如果你这样做了,当你从抽屉菜单中点击一个项目时,你是否输入了 onNavigationItemSelected() 方法?
-
嗨 matdev,是的,我做到了。 onNavigationItemSelected 发布在我的原始帖子中。该方法有效,我可以处理用户在菜单列表中的点击。问题是我不想调用新活动,我想更改片段(目前是谷歌地图)。
标签: java android android-fragments navigation-drawer