如何实现底部导航栏的跳转,而不是到片段,而是到活动类?

huangapple go评论74阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年8月20日 15:45:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63500522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定