BottomNavigationView - How to uncheck all MenuItems and keep Titles being displayed?

后端 未结 10 1237
面向向阳花
面向向阳花 2020-12-18 19:33

As I liked the design from BottomNavigationView I decided to implement a new Menu for my App with it, instead of just using simple buttons.

I took this

10条回答
  •  有刺的猬
    2020-12-18 20:09

    I found my own solution merging my progress with this post.

    Steps:

    1. Update proguard-rules.pro and sync build
    2. Create Helper to disable BottomNavigationView Shift Mode
    3. Create an Item to hide on Menu.xml
    4. Inflate BottomNavigationView
    5. Set Item to be hidden as Checked GONE
    6. Use Helper to disable Shifting Mode

    Output:

    Working code:

    proguard-rules.pro:

    -keepclassmembers class android.support.design.internal.BottomNavigationMenuView {
        boolean mShiftingMode;
    }
    

    BottomNavigationShiftHelper.java:

    public class BottomNavigationShiftHelper {
    
        private final static String TAG = "DEBUG_BOTTOM_NAV_UTIL";
    
        public static void disableShiftMode(BottomNavigationView view) {
            BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
            try {
                Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
                shiftingMode.setAccessible(true);
                shiftingMode.setBoolean(menuView, false);
                shiftingMode.setAccessible(false);
                for (int i = 0; i < menuView.getChildCount(); i++) {
                    BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                    item.setShiftingMode(false);
                    // set once again checked value, so view will be updated
                    item.setChecked(item.getItemData().isChecked());
                }
            } catch (NoSuchFieldException e) {
                Log.d(TAG, "Unable to get shift mode field");
            } catch (IllegalAccessException e) {
                Log.d(TAG, "Unable to change value of shift mode");
            }
        }
    }
    

    Activity Sample.java:

     private void loadNavigationBar() {
            BottomNavigationView bottomNavigationView = (BottomNavigationView)
                    findViewById(R.id.navigation_bar);
    
            bottomNavigationView.getMenu().findItem(R.id.uncheckedItem).setChecked(true);
            bottomNavigationView.findViewById(R.id.uncheckedItem).setVisibility(View.GONE);
    
            BottomNavigationViewUtils.disableShiftMode(bottomNavigationView);
    
            bottomNavigationView.setOnNavigationItemSelectedListener(
                    new BottomNavigationView.OnNavigationItemSelectedListener() {
                        @Override
                        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.newList:
                                    //Do The Math
                                    break;
                                case R.id.loadList:
                                    //Do The Math
                                    break;
                                case R.id.settings:
                                    //Do The Math
                                    break;
                            }
                            return false;
                        }
                    });
        }
    

    BottomNavigationMenu.xml:

    
    
        
        
        
        
    
    

    BottomNavigationComponent (inside Activity.xml):

    
    

提交回复
热议问题