简体   繁体   English

“默认”底部导航视图如何更改片段?

[英]How does 'default' bottom navigation view changes fragments?

I've seen many articles how to implement bottom navigation view, but none of those solutions looked like one from new project created with Bottom Navigation 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);

There are no transactions, no FragmentManager etc. 没有交易,没有FragmentManager等。

Reason I'm asking is because I'm trying to implement button on Fragment2 (PresetsFragment), which after click sets values and switches view to Fragment1 (TimerFragment). 我问的原因是因为我正在尝试在Fragment2(PresetsFragment)上实现按钮,单击后会设置值并将视图切换到Fragment1(TimerFragment)。 This is implementation of interface in MainActivity: 这是MainActivity中接口的实现:

@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();
    }
}

After adding logs I can see that interface works and new `TimerFragment' is created - but fragment itself doesn't change. 添加日志后,我可以看到该接口可以正常工作,并且创建了新的“ TimerFragment”-但片段本身没有改变。

Actually comment of John Joe helped me a lot. 实际上,对约翰·乔的评论对我有很大帮助。 There is no reason to use interface between fragments if I use Navigation . 如果使用Navigation则没有理由在片段之间使用接口。

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);

In my example this is all that is needed. 在我的示例中,这就是所需要的。 Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除带有片段的底部导航视图 - Remove Bottom Navigation view with fragments 我如何使用引用片段和活动的底部导航视图? - How can i use bottom navigation view referring to fragments and activities? 如何在底部导航片段(或导航抽屉)之间传递数据? - How to pass data between Bottom Navigation fragments (or Navigation Drawer)? 使用按钮 onClick 在片段之间切换,但底部导航图标不会改变 - Switching between fragments with button onClick but bottom navigation icon does not change 使用底部导航按钮切换时如何不破坏打开的片段 - how to not detsroy the fragments opened when switching with bottom navigation buttons 如何不使用 Android 中的底部导航栏导航回某些片段 - How to not navigate back to certain fragments using a bottom navigation bar in Android Hilt更改片段名称时如何使用导航组件 - How to use navigation component when Hilt changes the names of the fragments 如何在默认Android Studio导航抽屉中的片段之间切换 - How to switch between Fragments in default Android Studio Navigation Drawer 使用操作栏和底部导航时片段重叠 - Fragments overlap when using actionbar and bottom navigation 底部导航栏不切换片段 - Bottom Navigation bar doesn't switch Fragments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM