2017-08-02 60 views
0

當應用程序第一次啓動時,它檢查最後一個菜單項,就像它被點擊一樣。它顯示第一個菜單項內容。在Android Studio中,不會顯示任何錯誤,警告或日誌,提示出現問題。BottomNavigationView不斷檢查最後一個菜單項(當它啓動時),但顯示第一個菜單項

一旦應用程序運行,如果我點擊任何其他菜單項,它工作正常。我可以點擊每個菜單項,並相應地填充正確的片段。

這裏是MainActivity.java

public class MainActivity 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_main); 

     mBottomNav = (BottomNavigationView) 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 
    /* This is never called on start up */ 
    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: 
       frag = HomeFragment.newInstance(); 
       break; 
      case R.id.menu_archives: 
       frag = ArchivesFragment.newInstance(); 
       break; 
      case R.id.menu_inspirational: 
       frag = InspirationalFragment.newInstance(); 
       break; 
      case R.id.menu_search: 
       frag = SearchFragment.newInstance(); 
       break; 
     } 

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

     // unchecked the other items. 
     //THIS LINE HERE 
     for (int i = 0; i < mBottomNav.getMenu().size(); i++) { 
      MenuItem menuItem = mBottomNav.getMenu().getItem(i); 
      menuItem.setChecked(menuItem.getItemId() == mSelectedItem); 
     } 

     updateToolbarText(item.getTitle()); 

     if (frag != null) { 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      ft.add(R.id.container, frag, frag.getTag()); 
      ft.commit(); 
     } 
    } 

    private void updateToolbarText(CharSequence text) { 
     ActionBar actionBar = getSupportActionBar(); 
     if (actionBar != null) { 
      actionBar.setTitle(text); 
     } 
    } 

這裏是activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:design="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.ourdailystrength.mobile.android.MainActivity"> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#f1f1f1"> 

    </FrameLayout> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" 
     design:menu="@menu/bottom_nav_items" /> 
</LinearLayout> 

這裏是bottom_nav_items.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/menu_home" 
     android:title="@string/menu_home" 
     android:icon="@drawable/ic_home_black_48dp" 
     android:checked="true" <!-- Doesn't matter if this line is included or not --> 
     app:showAsAction="ifRoom"/> 

    <item 
     android:id="@+id/menu_archives" 
     android:title="@string/menu_archives" 
     android:icon="@drawable/ic_archive_black_48dp" 
     app:showAsAction="ifRoom"/> 

    <item 
     android:id="@+id/menu_search" 
     android:title="@string/menu_search" 
     android:icon="@drawable/ic_search_black_48dp" 
     app:showAsAction="ifRoom"/> 

    <item 
     android:id="@+id/menu_inspirational" 
     android:title="@string/menu_inspirational" 
     android:icon="@drawable/ic_photo_black_48dp" 
     app:showAsAction="ifRoom"/> 
</menu> 

下面是截圖時,第一次啓動時:

enter image description here

爲什麼要檢查最後一個菜單項而不是第一個菜單項?

編輯: 所以,從下面給出的答案,我做了這個變化,它的工作原理:

private void selectFragment(MenuItem item) { 
    Fragment frag = null; 

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

    // unchecked the other items. 
    for (int i = 0; i < mBottomNav.getMenu().size(); i++) { 
     MenuItem menuItem = mBottomNav.getMenu().getItem(i); 
     menuItem.setChecked(false); 
    } 

    // init corresponding fragment 
    switch (mSelectedItem) { 
     case R.id.menu_home: 
      frag = HomeFragment.newInstance(); 
      mBottomNav.getMenu().findItem(R.id.menu_home).setChecked(true); 
      break; 
     case R.id.menu_archives: 
      frag = ArchivesFragment.newInstance(); 
      mBottomNav.getMenu().findItem(R.id.menu_archives).setChecked(true); 
      break; 
     case R.id.menu_inspirational: 
      frag = InspirationalFragment.newInstance(); 
      mBottomNav.getMenu().findItem(R.id.menu_inspirational).setChecked(true); 
      break; 
     case R.id.menu_search: 
      frag = SearchFragment.newInstance(); 
      mBottomNav.getMenu().findItem(R.id.menu_search).setChecked(true); 
      break; 
    } 

    updateToolbarText(item.getTitle()); 

    if (frag != null) { 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.add(R.id.container, frag, frag.getTag()); 
     ft.commit(); 
    } 
} 

爲什麼會在上面的代碼THIS LINE HERE因爲它做到這一點奇怪的錯誤?

回答

0

希望這可以幫助您解決您的問題。 無論何時更改Fragment,請調用此方法。根據您的需要通過checkedcolorunchecked color

private void changeMenuItemCheckedStateColor(int checkedColor, int uncheckedColor) { 
     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); 
    } 
0

有2個解決方案

1)使用集團

<group android:checkableBehavior="single"> 
    <item .../> 
    <item .../> 
    <item .../> 
</group> 

2)通過編程

Menu bottomMenu; 
BottomNavigationView bottomNavigationMenu = (BottomNavigationView) findViewById(R.id.bottom_navigation); 
bottomMenu = navigation.getMenu(); 
bottomMenu.findItem(R.id.menu_home).setChecked(true); 

做是希望它會幫助,如果您有任何查詢請評論。在BottomNavigationView

// unchecked the other items. 
     for (int i = 0; i < mBottomNav.getMenu().size(); i++) { 
      MenuItem menuItem = mBottomNav.getMenu().getItem(i); 
      menuItem.setChecked(menuItem.getItemId() == mSelectedItem); 
     } 

,並給予着色顏色:

+1

請參閱編輯。爲什麼這段代碼會導致這種情況? –

0

刪除下面的代碼

<android.support.design.widget.BottomNavigationView 
     android:id="@+id/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" 
     app:itemIconTint="@drawable/nav_item_color_state" 
     design:menu="@menu/bottom_nav_items" /> 
+0

參見編輯。爲什麼這段代碼會導致這種情況? –

相關問題