2016-12-29 76 views
1

因爲我喜歡BottomNavigationView的設計,所以我決定用它爲我的應用程序實現一個新的菜單,而不是使用簡單的按鈕。BottomNavigationView - 如何取消選中所有MenuItems並保持標題顯示?

我把this作爲指導。

根據BottomNavigationViewdocumentation,其目的是爲了

provide quick navigation between top-level views of an app. It is primarily designed for use on mobile.

在我而言,我只是希望每個MenuItem推出一個活動,但默認情況下總是存在一個MenuItem選擇:

enter image description here

我試圖將顏色設置爲白色:

app:itemIconTint="@color/white" 
app:itemTextColor="@color/white" 

不過,明顯的選擇MenuItem不同於他人(標題大小大),這仍然是困擾我:

enter image description here

我的想法來到放置一個隱藏MenuItem選擇,如:

<item 
android:id="@+id/uncheckedItem" 
android:title="" /> 

,使視圖GONE

bottomNavigationView.getMenu().findItem(R.id.uncheckedItem).setChecked(true); 
bottomNavigationView.findViewById(R.id.uncheckedItem).setVisibility(View.GONE); 

這使得所有的MenuItems不加以控制,但默認情況下BottomNavigationView是hidding標題,因爲它有3周以上的MenuItems顯示,就算第四MenuItem是定居於GONE

enter image description here

所以我的遺骸問題,是否存在/破解取消選擇所有MenuItem並保持其標題顯示?

回答

2

我發現我自己的解決方案將我的進度與this後合併。

步驟:

  1. 更新proguard-rules.pro和同步建設
  2. 創建助手禁用BottomNavigationView換檔模式
  3. 創建一個項目來隱藏菜單。XML
  4. 膨脹BottomNavigationView
  5. 設置項被隱藏作爲託運GONE
  6. 使用助手禁用移動模式

輸出:

enter image description here

工作代碼:

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

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

博特omNavigationMenu.xml:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/newList" 
     android:enabled="true" 
     android:icon="@drawable/new_list" 
     android:title="@string/common.button.list.new" 
     app:showAsAction="withText" /> 
    <item 
     android:id="@+id/loadList" 
     android:enabled="true" 
     android:icon="@drawable/load" 
     android:title="@string/common.button.list.load" 
     app:showAsAction="withText" /> 
    <item 
     android:id="@+id/settings" 
     android:enabled="true" 
     android:icon="@drawable/settings" 
     android:title="@string/common.label.settings" 
     app:showAsAction="withText" /> 
    <item 
     android:id="@+id/uncheckedItem" 
     android:title="" /> 
</menu> 

BottomNavigationComponent(內Activity.xml):

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation_bar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    app:itemIconTint="@color/white" 
    app:itemTextColor="@color/white" 
    android:background="@drawable/BottomNavigationMenu.xml" 
    app:menu="@menu/supercart_bottom_navigation" /> 
+0

它的工作原理,但現在我的底部導航視圖並沒有採取一切寬......可以請你協助 – TheGreat004

+0

我得到了以下問題,https://github.com/ittianyu/BottomNavigationViewEx/issues/25 – TheGreat004

+1

我有5個項目,如果我添加一個空項目,我得到這個錯誤:BottomNavigationView支持的項目的最大數量是5 – Kusan

1

您的解決方案似乎改變項目之間的間隔

有我的解決方案:

「只需將點擊的顏色設置爲與未點擊的顏色相同的顏色即可。」

例如:

private void changeMenuItemCheckedStateColor(BottomNavigationView bottomNavigationView, String checkedColorHex, String uncheckedColorHex) { 
    int checkedColor = Color.parseColor(checkedColorHex); 
    int uncheckedColor = Color.parseColor(uncheckedColorHex); 

    int[][] states = new int[][] { 
      new int[] {-android.R.attr.state_checked}, // unchecked 
      new int[] {android.R.attr.state_checked}, // checked 

    }; 

    int[] colors = new int[] { 
      uncheckedColor, 
      checkedColor 
    }; 

    ColorStateList colorStateList = new ColorStateList(states, colors); 

    bottomNavigationView.setItemTextColor(colorStateList); 
    bottomNavigationView.setItemIconTintList(colorStateList); 

} 

,如果你想取消勾選所有項目,你可以

changeMenuItemCheckedStateColor(mBottomNavigationView, "#999999", "#999999"); 
如果你想恢復顏色設置

,可以

changeMenuItemCheckedStateColor(mBottomNavigationView, "FF743A", "999999"); 
3

感謝您的想法。我已經在lib中實現了它。 我有更好的辦法來反思。所以它不會顯示空間。

如果你有興趣。點擊這裏:https://github.com/ittianyu/BottomNavigationViewEx

private void initBottomViewAndLoadFragments(final BottomNavigationViewEx bnve) { 
    bnve.enableAnimation(false); 
    bnve.enableShiftingMode(false); 
    bnve.enableItemShiftingMode(false); 

    // use the unchecked color for first item 
    bnve.setIconTintList(0, getResources().getColorStateList(R.color.bnv_unchecked_black)); 
    bnve.setTextTintList(0, getResources().getColorStateList(R.color.bnv_unchecked_black)); 

    bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { 

     private boolean firstClick = true; 
     private int lastItemId = -1; 

     @Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
      // restore the color when click 
      if (firstClick) { 
       firstClick = false; 
       bnve.setIconTintList(0, getResources().getColorStateList(R.color.selector_bnv)); 
       bnve.setTextTintList(0, getResources().getColorStateList(R.color.selector_bnv)); 
      } 

      if (firstClick || lastItemId == -1 || lastItemId != item.getItemId()) { 
       lastItemId = item.getItemId(); 
      } else { 
       return false; 
      } 

      // do stuff 
      return fillContent(item.getItemId()); 
     } 
    }); 
} 

- RES /彩色/ selector_bnv.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/bnv_checked_white" android:state_checked="true" /> 
    <item android:color="@color/bnv_unchecked_black" /> 
</selector> 

- RES /價值/顏色。XML

<color name="bnv_checked_white">@android:color/white</color> 
<color name="bnv_unchecked_black">@android:color/black</color> 
+0

幹得好!請寫下如何讓第一次不受檢查。沒有在您的回購中找到此代碼。 – oxied

+0

更新:發現在他的回購:https://github.com/ittianyu/BottomNavigationViewEx/blob/master/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/style/StyleActivity.java#L152 – oxied

+0

也更新他的直接代碼示例。 – oxied

0

這是一樣的接受的答案除我已經改變的代碼兩行下面標記。在循環BottomNavigationItemViews時,我始終將檢查設置爲false,並且還將checkable設置爲false。這可以防止菜單項目改變大小。你仍然需要這個proguard的規則:

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

更新代碼:

static void removeShiftMode(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); 
       item.setChecked(false); // <--- This line changed 
       item.setCheckable(false); // <-- This line was added 
      } 
     } 
     catch (NoSuchFieldException e) 
     { 
      Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field"); 
     } 
     catch (IllegalAccessException e) 
     { 
      Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode"); 
     } 

    } 
相關問題