簡體   English   中英

“默認”底部導航視圖如何更改片段?

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

我看過很多文章如何實現底部導航視圖,但是這些解決方案都不像使用底部導航活動創建的新項目中的解決方案:

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)上實現按鈕,單擊后會設置值並將視圖切換到Fragment1(TimerFragment)。 這是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();
    }
}

添加日志后,我可以看到該接口可以正常工作,並且創建了新的“ TimerFragment”-但片段本身沒有改變。

實際上,對約翰·喬的評論對我有很大幫助。 如果使用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);

在我的示例中,這就是所需要的。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM