2016-11-15 70 views
0

我正面臨着一個奇怪的問題,即最新發布的Bottom Navigation View(在設計支持庫中)。如果您有超過3個選項卡(我有5個選項卡),並且在設備或模擬器的開發人員選項中啓用了don't keep activities,並且您在具有底部導航視圖的活動之上啓動新活動(可能來自一個),並在幾秒鐘後關閉它(稍等一會,以確保舊的活動被Android殺死),底部導航視圖中所有選項卡的標籤更改爲最後一個選項卡的標籤,即您有一個標籤,最後一個標籤的標籤,在所有標籤中重複。這個問題很奇怪,因爲菜單是從定義了圖標和標籤的菜單xml文件中膨脹的,但圖標顯示正確,而標籤沒有顯示。我感謝任何幫助。底部導航視圖中的標籤問題

我報告了這個錯誤。如果您遇到同樣的問題,請對錯誤here進行加星標以幫助確定錯誤修復優先級。

+0

你是什麼意思的不正確顯示?你可以張貼截圖嗎? –

回答

0

問題出在freezeText屬性。如果你已經在你的styles.xml啓用freezeText全局(爲所有TextViews的),像這樣:

<style name="Text" parent="android:Widget.TextView"> 
    <item name="android:freezesText">true</item> 
</style> 

需要,因爲它在池中緩存TextViews禁用由BottomNavigationView使用的TextViews這個屬性,從池中重用它們。此視圖也有一個錯誤:即使偵聽器(類型OnNavigationItemSelectedListener)返回false,它始終返回true。如果偵聽程序返回false(基於某些條件檢查,例如用戶未登錄),如果您不想選擇新選項卡,則會導致問題。這也可以通過直接調用監聽器的onNavigationItemSelected()來解決。我們添加了以下幫助方法來解決這兩個問題:

private void setNavigationItemSelectedListener(BottomNavigationView.OnNavigationItemSelectedListener listener) { 
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0); 
    for (int i = 0; i < menuView.getChildCount(); i++) { 
    BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i);  

    // Labels are re-used from a pool, if you have global freeze text enabled, this causes state problems 
    ((TextView) itemView.findViewById(android.support.design.R.id.smallLabel)).setFreezesText(false); 
    ((TextView) itemView.findViewById(android.support.design.R.id.largeLabel)).setFreezesText(false); 

    // Workaround for BottomNavigationMenu bug where it selects the item even if the listener returns false 
    itemView.setOnClickListener(click -> listener.onNavigationItemSelected(itemView.getItemData())); 
    } 
}