BottomNavigationView中片段之间的过渡

时间:2019-02-13 23:42:07

标签: java android android-fragments bottomnavigationview

在片段之间导航时,当前片段显示在底部(图标突出显示)。但是,当您按下“后退”按钮并在堆栈上移动时,底部导航不起作用,并且冻结了最后一个手动按下的元素。如何解决?

public class MainActivity extends AppCompatActivity {

    Fragment selectedFragment = new AFragment();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.nav_home:
                    selectedFragment = new AFragment();
                    break;
                case R.id.nav_search:
                    selectedFragment = new BFragment();
                    break;
                case R.id.nav_add:
                    startActivity(new Intent(getBaseContext(), CreateMeetActivity.class));
                    setBorderActive(v_3);
                    break;
                case R.id.nav_notifications:
                    selectedFragment = new CFragment();
                    break;
                case R.id.nav_profile:
                    selectedFragment = new DFragment();
                    break;
                default:
                    break;
            }
            getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, selectedFragment).commit();
            return true;
        }
    };

}

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我将使用 Developer Option (开发人员选项) 显示布局范围来查看“底部导航”选项卡中发生的情况。

开发人员选项下-> 工程图-> 显示布局范围,然后将其打开。然后,您将能够看到布局发生了什么。

然后您将获得类似于以下内容的信息:

screenshot of the developer option - show layout bounds