1

目前我有像這個 -我可以禁止在特定邏輯的BottomNavigationView項目中移動嗎?

mBottomNav.setOnNavigationItemSelectedListener(
       ...... 
    switch (item.getItemId()) { 

     case R.id.actionUser: 
      if(userLoggedIn) { 
       mHomePresenter.btnUser(); 
      } 
      else { 
       showLongToast("User Not Logged In"); 
      } 
      break; 
    }); 

邏輯其他部分的實現,我將顯示敬酒消息,無論是我想要的BottomNavigationView轉移也沒有菜單圖標的顏色變化。

如何才能在這個特定的部分實現這樣的邏輯?所有其他菜單項將保持默認的切換邏輯。

+0

嗨法蒂瑪 你能否提供更多信息?目前尚不清楚。如果你能提供一些運行的例子,這將是非常好的。 –

+0

我已經有了一個解決方案。見下面接受的答案。感謝你的關心! @ShravanJain – Farwa

+0

Awesome Yrr .... :) –

回答

1

那麼答案很簡單,對於條件你要過渡到發生,因爲你不想返回false

以你的代碼考慮條件返回true,它應該是

mBottomNav.setOnNavigationItemSelectedListener(
    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.actionUser: 
      if(userLoggedIn) { 
       mHomePresenter.btnUser(); 
       return true; 
      } 
      else { 
       showLongToast("User Not Logged In"); 
       return false; 
      } 
     } 
    }); 

如果檢查導航選擇監聽器的文件,你可以看到

/** 
* Listener for handling selection events on bottom navigation items. 
*/ 
public interface OnNavigationItemSelectedListener { 

    /** 
    * Called when an item in the bottom navigation menu is selected. 
    * 
    * @param item The selected item 
    * 
    * @return true to display the item as the selected item and false if the item should not 
    *   be selected. Consider setting non-selectable items as disabled preemptively to 
    *   make them appear non-interactive. 
    */ 
    boolean onNavigationItemSelected(@NonNull MenuItem item); 
} 
+0

精彩!!有效!我是初學者。那個轉變的助手課正在沸騰我的頭! 感謝您指出這個最簡單的解決方案! – Farwa

+1

很高興:) :) @FatimaMostafa快樂編碼。 – Sanoop

相關問題