Android - 导航抽屉 - 与动态菜单项重叠的片段
Android - Navigation Drawer - Fragments overlapping with dynamic meny items
我正在动态添加城市名称作为菜单项,所有这些都指向 Home Fragment。但是,当我 select 一个城市名称,然后是任何其他菜单选项时,两个片段重叠,如下图所示。
Fragments overlapping
Navigation Drawer
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
menuItem = navigationView.getMenu().findItem(R.id.locations);
addCityToMenu("London");
addCityToMenu("New York");
addCityToMenu("Milan");
addCityToMenu("Paris");
addCityToMenu("Monaco");
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_locations, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
private void addCityToMenu(String city) {
menuItem.getSubMenu().add(Menu.NONE, R.id.nav_home, 1, city)
.setOnMenuItemClickListener(item -> loadCity(city));
}
public boolean loadCity(String city) {
Toast.makeText(getApplicationContext(), city, Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("cityName", city);
HomeFragment fragment = new HomeFragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.commit();
drawer.closeDrawers();
return true;
}
fragment_home
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#121010"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment.java
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
if(getArguments()!=null) {
homeViewModel.setText(getArguments().getString("cityName"));
}
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
所有代码都可以在 https://github.com/vishnu-android/DynamicNavigationDrawer
正如您提到的,您遇到了片段重叠问题。我有一个解决方案。
XML部分
1)删除mobile_navigation.xml
2) 您需要将 app:navGraph="@navigation/mobile_navigation" 替换为您的默认片段名称 tools:layout="@layout/fragment_home" 位于 content_main.xml 文件中。
之前:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
之后:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/fragment_home" />
MainActivity.java
1) 删除默认导航代码
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_locations, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
2) 添加自定义抽屉菜单
// set custom toggle menu to open and close drawer
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
//set menu icon
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_baseline_menu_24);
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawer.openDrawer(GravityCompat.START);
}
});
3) 将默认片段设置为主页片段
//set default fragment which will open first
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new HomeFragment()).commit();
}
4) 设置 NavigationItemSelectedListener
//set navigation menu item click event
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new HomeFragment()).commit();
drawer.closeDrawers();
break;
case R.id.nav_gallery:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new GalleryFragment()).commit();
drawer.closeDrawers();
break;
case R.id.nav_slideshow:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new SlideshowFragment()).commit();
drawer.closeDrawers();
break;
default:
break;
}
return true;
}
});
我正在动态添加城市名称作为菜单项,所有这些都指向 Home Fragment。但是,当我 select 一个城市名称,然后是任何其他菜单选项时,两个片段重叠,如下图所示。
Fragments overlapping
Navigation Drawer
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
menuItem = navigationView.getMenu().findItem(R.id.locations);
addCityToMenu("London");
addCityToMenu("New York");
addCityToMenu("Milan");
addCityToMenu("Paris");
addCityToMenu("Monaco");
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_locations, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
private void addCityToMenu(String city) {
menuItem.getSubMenu().add(Menu.NONE, R.id.nav_home, 1, city)
.setOnMenuItemClickListener(item -> loadCity(city));
}
public boolean loadCity(String city) {
Toast.makeText(getApplicationContext(), city, Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("cityName", city);
HomeFragment fragment = new HomeFragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.commit();
drawer.closeDrawers();
return true;
}
fragment_home
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#121010"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment.java
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
if(getArguments()!=null) {
homeViewModel.setText(getArguments().getString("cityName"));
}
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
所有代码都可以在 https://github.com/vishnu-android/DynamicNavigationDrawer
正如您提到的,您遇到了片段重叠问题。我有一个解决方案。
XML部分
1)删除mobile_navigation.xml
2) 您需要将 app:navGraph="@navigation/mobile_navigation" 替换为您的默认片段名称 tools:layout="@layout/fragment_home" 位于 content_main.xml 文件中。
之前:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
之后:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/fragment_home" />
MainActivity.java
1) 删除默认导航代码
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_locations, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
2) 添加自定义抽屉菜单
// set custom toggle menu to open and close drawer
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
//set menu icon
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_baseline_menu_24);
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawer.openDrawer(GravityCompat.START);
}
});
3) 将默认片段设置为主页片段
//set default fragment which will open first
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new HomeFragment()).commit();
}
4) 设置 NavigationItemSelectedListener
//set navigation menu item click event
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new HomeFragment()).commit();
drawer.closeDrawers();
break;
case R.id.nav_gallery:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new GalleryFragment()).commit();
drawer.closeDrawers();
break;
case R.id.nav_slideshow:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new SlideshowFragment()).commit();
drawer.closeDrawers();
break;
default:
break;
}
return true;
}
});