gpt4 book ai didi

java - 需要有关抽屉导航过渡的帮助

转载 作者:行者123 更新时间:2023-12-02 01:50:31 25 4
gpt4 key购买 nike

我设置了一个抽屉导航,首先它在 ocCreate 方法中显示一个 Google map 。我想按抽屉导航菜单中的另一个按钮并更改 map fragment 。

我有一个 Activity_nav.xml 和一个 app_bar_nav.xml,其中包含 content_nav.xml。

我遵循了谷歌地图 API 网站,以便在谷歌开发人员上实现 map 和抽屉导航部分。

现在我被困在这里,因为我不知道如何切换到同一抽屉导航内的另一个 fragment 。

我的 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;
}

最佳答案

要在 fragment 之间切换,您的 Activity 布局应具有容器布局:

 <FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

在 Activity 的 onCreate() 方法中,将容器替换为默认 fragment ,即 map fragment :

getSupportFragmentManager().beginTransaction().replace(R.id.container, mapFragment).commit();

然后在 onNavigationItemSelected() 中,将当前 fragment 替换为所选项目的目标 fragment :

Fragment fragment = Fragment.instantiate(this, YourTargetFragment.class.getName().getTitle());
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
setTitle(item.getTitle());

关于java - 需要有关抽屉导航过渡的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57430900/

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