我看过很多如何实现底部导航 View 的文章,但这些解决方案都不像使用底部导航 Activity 创建的新项目中的解决方案:
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration.Builder(
R.id.navigation_timer, R.id.navigation_presets)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
没有事务,没有 FragmentManager
等。
我问的原因是因为我试图在 Fragment2 (PresetsFragment) 上实现按钮,单击后设置值并将 View 切换到 Fragment1 (TimerFragment)。 这是MainActivity中接口(interface)的实现:
@Override
public void updateCountdowns(final int round_time, final int break_time)
{
TimerFragment timer = (TimerFragment)getSupportFragmentManager().findFragmentById(R.id.navigation_timer);
if (timer != null) {
timer.updateCountdowns(round_time, break_time);
} else
{
TimerFragment new_timer = new TimerFragment();
Bundle args = new Bundle();
args.putInt("round_time_bundle", round_time);
args.putInt("break_time_bundle", break_time);
new_timer.setArguments(args);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.navigation_presets, new_timer, "new_timer")
.addToBackStack(null)
.commit();
}
}
添加日志后,我可以看到界面正常工作并且创建了新的“TimerFragment” - 但 fragment 本身没有改变。
最佳答案
实际上约翰·乔的评论对我帮助很大。如果我使用导航
,则没有理由在 fragment 之间使用接口(interface)。
Bundle args = new Bundle();
args.putInt("round_time_bundle", round_time);
args.putInt("break_time_bundle", break_time);
Navigation.findNavController(view).navigate(R.id.navigation_timer, args);
在我的示例中,这就是所需的全部内容。 谢谢。
关于java - 'default' 底部导航 View 如何更改 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946832/