How to Set selected item in BottomNavigationView

前端 未结 12 1835
半阙折子戏
半阙折子戏 2020-12-25 11:48

I am trying to set default item on activity created but it isn\'t working? This is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.o         


        
相关标签:
12条回答
  • 2020-12-25 12:02

    Kotlin solution with Fragment

    var fragment = Fragment()
    bottomNav = findViewById(R.id.bottom_nav)
    bottomNav.setOnNavigationItemSelectedListener { item ->
      when (item.itemId) {
        R.id.home -> {
          fragment = HomeForm()
        }
        R.id.score -> {
          fragment = ScoreForm()
        }
      }
      supportFragmentManager
        .beginTransaction()
        .replace(R.id.frame_layout, fragment)
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        .commit()
      true
    }
    bottomNav.selectedItemId = R.id.score // init open score tab
    
    0 讨论(0)
  • 2020-12-25 12:04

    If what you want is that the clicked element is ignored on the view and is not returned as "selected" you can return false after the click is handled, in some cases and some designs you could need to open an activity instead of a fragment and this will make selected the bottom item after the activity is closed.

    private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_shipment -> {
                currentItem = TAB_INDEX_SHIPMENT
                val intent = Intent(this, BookShipmentActivity::class.java)
                startActivity(intent)
                return@OnNavigationItemSelectedListener false
            }
        }
        false
    }
    
    0 讨论(0)
  • 2020-12-25 12:05

    Just share my working source code

    In Xml,

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.BottomNavigationView
            android:background="@color/colorWhite"
            android:id="@+id/gfPrlBnvBtmView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_alignParentBottom="true"
            app:menu="@menu/bottom_navigation_main" />
    </LinearLayout>
    

    In Java,

      public class TestActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener
    {
        private BottomNavigationView mBtmView;
        private int mMenuId;
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
            super.onCreate(savedInstanceState, persistentState);
            setContentView(R.layout.test);
            mBtmView = (BottomNavigationView) findViewById(R.id.gfPrlBnvBtmView);
            mBtmView.setOnNavigationItemSelectedListener(this);
            mBtmView.getMenu().findItem(R.id.action_yoga).setChecked(true);
        }
    
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            // uncheck the other items.
            mMenuId = item.getItemId();
            for (int i = 0; i < mBtmView.getMenu().size(); i++) {
                MenuItem menuItem = mBtmView.getMenu().getItem(i);
                boolean isChecked = menuItem.getItemId() == item.getItemId();
                menuItem.setChecked(isChecked);
            }
    
            switch (item.getItemId()) {
                case R.id.action_food: {
                }
                break;
                case R.id.action_medical: {
                }
                break;
                case R.id.action_yoga: {
                }
                break;
                case R.id.action_postures: {
                }
                break;
            }
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-12-25 12:06

    There are two senario

    Set the selected menu item ID. This behaves the same as tapping on an item.

    Code example:

    BottomNavigationView bottomNavigationView;
    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
    bottomNavigationView.setOnNavigationItemSelectedListener(myNavigationItemListener);
    bottomNavigationView.setSelectedItemId(R.id.my_menu_item_id);
    

    I you only need to check it without tapping

    Code example:

      BottomNavigationView bottomNavigationView= (getActivity()).findViewById(R.id.bottom_navigation);
        bottomNavigationView.getMenu().findItem(R.id.action_manage_orders_id).setChecked(true);
    
    0 讨论(0)
  • 2020-12-25 12:09

    This works for me

    Activity layout:

    <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@color/tabs"
            app:itemTextColor="@color/tabs"
            app:menu="@menu/bottom_navigation_main" />
    

    color/tabs.xml:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:color="@color/not_active" android:state_checked="false"/>
        <item android:color="@color/active" android:state_checked="true"/>
    
    </selector>
    

    Click callback:

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_tab0:
                setFragment(f0);
                break;
            case R.id.action_tab1:
                setFragment(f1);
                break;
        }
        return true; // not false!
    }
    
    0 讨论(0)
  • 2020-12-25 12:09

    Add android:enabled="true" to your BottomNavigation menu items.

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
      <item
        android:id="@+id/menu_id"
        android:enabled="true"
        android:icon="@drawable/ic_my_icon"
        android:title="@string/menu_title"/>
    </menu>
    

    And in onCreate() method, set up listeners by bottomNavigationView.setOnNavigationItemSelectedListener(mListener).

    And set the desired item to be selected by doing bottomNavigationView.selectedItemId = R.id.menu_id.

    This will trigger onNavigationItemSelected() from the NavigationItemSelectedListener whenever the activity is created.

    0 讨论(0)
提交回复
热议问题