我已经按照下面的教程进行了操作,但是对于我的生活来说,无法从收藏夹片段(bottomnavigationview上的中间图标)开始加载它。
我已经尝试了一切,到处寻找。
我的应用程序将始终加载收藏夹文本,但是在我选择图标之前,bottomnavigationview将始终突出显示主页图标。
我如何解决这个问题,我可以让应用程序打开并显示最喜欢的片段,同时收藏夹图标突出显示并处于空状态?
https://codinginflow.com/code-examples/android/bottomnavigationview
答案 0 :(得分:1)
在您的MainActivity中,将其替换为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
}
用这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
bottomNav.setSelectedItemId(R.id.nav_favorites);
}
我们基本上只是将这一行添加到onCreate()函数的末尾:
bottomNav.setSelectedItemId(R.id.nav_favorites);