在Android中启动应用程序时,如何将片段设置为主页图标(底部导航)的默认片段

时间:2018-04-12 15:45:20

标签: android android-fragments

我想用bottomnavigation开发一个应用程序,在这里我有3个图标Home,Reminder,Settings。

我想在打开应用时将fragment设置为主页图标的默认片段。

我有三个片段Home Fragment,Reminder Fragment和Settings Fragment。

MainActivity

public class MainActivity extends AppCompatActivity
                          implements Home.OnFragmentInteractionListener,
                          Reminders.OnFragmentInteractionListener,
                          Settings.OnFragmentInteractionListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

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

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    switchToHomeFragment();
                    break;
                case R.id.navigation_dashboard:
                    switchToRemindersFragment();
                    break;
                case R.id.navigation_notifications:
                    switchToSettingsFragment();
                    break;
            }
            return false;
        }
    };

    public void switchToHomeFragment() {
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.content_frame, new Home()).commit();
    }
    public void switchToRemindersFragment() {
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.content_frame, new Reminders()).commit();
    }
    public void switchToSettingsFragment() {
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.content_frame, new Settings()).commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

2 个答案:

答案 0 :(得分:2)

这是我的代码,它完美无缺,为您服务。

public class MyActivity extends AppCompatActivity {        
            private static final String SELECTED_ITEM = "arg_selected_item";
            private BottomNavigationView mBottomNav;
            private int mSelectedItem;

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

                mBottomNav = findViewById(R.id.navigation);
                mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        selectFragment(item);
                        return true;
                    }
                });

                MenuItem selectedItem;
                if (savedInstanceState != null) {
                    mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
                    selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
                } else {
                    selectedItem = mBottomNav.getMenu().getItem(0);
                }
                selectFragment(selectedItem);
            }

            @Override
            protected void onSaveInstanceState(Bundle outState) {
                outState.putInt(SELECTED_ITEM, mSelectedItem);
                super.onSaveInstanceState(outState);
            }

            @Override
            public void onBackPressed() {
                MenuItem homeItem = mBottomNav.getMenu().getItem(0);
                if (mSelectedItem != homeItem.getItemId()) {
                    // select home item
                    selectFragment(homeItem);
                } else {
                    super.onBackPressed();
                }
            }

            private void selectFragment(MenuItem item) {
                Fragment frag = null;
                // init corresponding fragment
                switch (item.getItemId()) {
                    case R.id.menu_home:

                        getSupportFragmentManager().beginTransaction().replace(R.id.container, new HomeFragment()).commit();
                        break;
                    case R.id.menu_suggestion:

                        getSupportFragmentManager().beginTransaction().replace(R.id.container, new SuggestionFragment()).commit();
                        break;
                    case R.id.menu_notification:

                        getSupportFragmentManager().beginTransaction().replace(R.id.container, new NotificationFragment()).commit();
                        break;
                    case R.id.menu_profile:

                        getSupportFragmentManager().beginTransaction().replace(R.id.container, new ProfileFragment()).commit();
                        break;
                }

                // update selected item
                mSelectedItem = item.getItemId();


            }

            }

答案 1 :(得分:1)

只需在switchToHomeFragment()

中调用此方法onCreate即可
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    switchToHomeFragment()
    // ^^^^^^^^^^^ call this to display home fragment when activity is created
}

或者您可以在onCreate内使用setSelectedItemId

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

   // recommended method, will highlight the icon as well
   navigation.setSelectedItemId(R.id.navigation_home);
}