問題出在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()));
}
}
你是什麼意思的不正確顯示?你可以張貼截圖嗎? –