需要有关导航抽屉过渡的帮助
Need help on a Navigation Drawer transition
我设置了一个导航抽屉,首先它在 ocCreate 方法中显示了一个 Google 地图。我想按导航抽屉菜单中的另一个按钮并更改地图片段。
我有一个 activity_nav.xml 和一个 app_bar_nav.xml,其中包括 content_nav.xml。
我关注了 google 地图 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;
}
要在片段之间切换,您的 activity 布局应具有容器布局:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在您的 activity 的 onCreate() 方法中,将容器替换为默认片段,即地图片段:
getSupportFragmentManager().beginTransaction().replace(R.id.container, mapFragment).commit();
然后在onNavigationItemSelected()中,将当前片段替换为所选项目的目标片段:
Fragment fragment = Fragment.instantiate(this, YourTargetFragment.class.getName().getTitle());
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
setTitle(item.getTitle());
好的,我做到了。感谢 matdev :)
我听从了你的建议,基本上这个问题与我在 content_nav.xml 中插入的片段有关
我用 FrameLayout 替换了片段,然后用 SupportMapFragment 初始化地图。对于我需要的其他片段,我将创建一个 class,每个片段都有一个专用的 xml。
我不明白这个机制,但你的提示很宝贵。
我设置了一个导航抽屉,首先它在 ocCreate 方法中显示了一个 Google 地图。我想按导航抽屉菜单中的另一个按钮并更改地图片段。
我有一个 activity_nav.xml 和一个 app_bar_nav.xml,其中包括 content_nav.xml。
我关注了 google 地图 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;
}
要在片段之间切换,您的 activity 布局应具有容器布局:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在您的 activity 的 onCreate() 方法中,将容器替换为默认片段,即地图片段:
getSupportFragmentManager().beginTransaction().replace(R.id.container, mapFragment).commit();
然后在onNavigationItemSelected()中,将当前片段替换为所选项目的目标片段:
Fragment fragment = Fragment.instantiate(this, YourTargetFragment.class.getName().getTitle());
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
setTitle(item.getTitle());
好的,我做到了。感谢 matdev :)
我听从了你的建议,基本上这个问题与我在 content_nav.xml 中插入的片段有关 我用 FrameLayout 替换了片段,然后用 SupportMapFragment 初始化地图。对于我需要的其他片段,我将创建一个 class,每个片段都有一个专用的 xml。
我不明白这个机制,但你的提示很宝贵。