英文:
How to Implementing bottom navigation bar transaction not to fragment , however to Activity class?
问题
setContentView(R.layout.activity_main);
fm.beginTransaction().add(R.id.main_container, fragment3).hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2).hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container, fragment1).commit(); // 这是一个片段
// 这是实现片段的部分
final BottomNavigationView bottomNavigationView = findViewById(R.id.nav_bawah);
bottomNavigationView.setSelectedItemId(R.id.navigation_dashboard);
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
break;
case R.id.navigation_challange:
fm.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
break;
case R.id.navigation_chat:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
break;
}
return true;
}
});
英文:
How to transition to activity instead of a fragment, if I clicked bottom navigation item?
setContentView(R.layout.activity_main);
fm.beginTransaction().add(R.id.main_container,fragment3).hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container,fragment2).hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1).commit(); //this is fragment
//this is implementing fragment
final BottomNavigationView bottomNavigationView = findViewById(R.id.nav_bawah);
bottomNavigationView.setSelectedItemId(R.id.navigation_dashboard);
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected( MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
fm.beginTransaction().hide(active).show(fragment1).commit();
active=fragment1;
break;
case R.id.navigation_challange:
fm.beginTransaction().hide(active).show(fragment2).commit();
active=fragment2;
break;
case R.id.navigation_chat:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
break;
}
return true;
}
});
答案1
得分: 0
使用Intent
类启动一个新的活动。
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_related_to_activity:
Intent intent = new Intent(requireActivity(), YourActivity.class);
startActivity(intent);
break;
// 其他情况 ...
}
return true;
}
});
英文:
Use Intent
class to start a new activity.
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected( MenuItem item) {
switch (item.getItemId()) {
case R.id.item_related_to_activity:
Intent intent = new Intent(requireActivity(), YourActivity.class);
startActivity(intent);
break;
// other cases ...
}
return true;
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论