Destek kütüphanesinden yeni android.support.design.widget.BottomNavigationView
kullanıyorum.
Geçerli seçimi koddan nasıl ayarlayabilirim? Ekranı döndürdükten sonra seçimin ilk öğeye geri döndüğünü fark ettim. Tabii ki birisi bana onPause' fonksiyonunda
BottomNavigationView' mevcut durumunu nasıl "kaydedeceğimi" ve `onResume' fonksiyonunda nasıl geri yükleyeceğimi söyleyebilirse de yardımcı olacaktır.
Teşekkürler!
Bu muhtemelen gelecek güncellemelerde eklenecektir. Ancak bu arada, bunu gerçekleştirmek için yansıma kullanabilirsiniz.
BottomNavigationView'den uzanan özel bir görünüm oluşturun ve bazı alanlarına erişin.
public class SelectableBottomNavigationView extends BottomNavigationView {
public SelectableBottomNavigationView(Context context) {
super(context);
}
public SelectableBottomNavigationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SelectableBottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setSelected(int index) {
try {
Field f = BottomNavigationView.class.getDeclaredField("mMenuView");
f.setAccessible(true);
BottomNavigationMenuView menuView = (BottomNavigationMenuView) f.get(this);
try {
Method method = menuView.getClass().getDeclaredMethod("activateNewButton", Integer.TYPE);
method.setAccessible(true);
method.invoke(menuView, index);
} catch (SecurityException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
Ve sonra bunu xml düzen dosyanızda kullanın.
<com.your.app.SelectableBottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@color/primary"
app:itemIconTint="@drawable/nav_item_color_state"
app:itemTextColor="@drawable/nav_item_color_state"
app:menu="@menu/bottom_navigation_menu"/>
Kodunuzu değiştirmek istemiyorsanız. Eğer öyleyse, BottomNavigationViewEx。 denemenizi tavsiye ederim.
Sadece setCurrentItem(index);
ve getCurrentItem()
yöntemlerini değiştirmeniz gerekir。
Yansıma kötü bir fikirdir.
Bu gist'e gidin. Seçimi gerçekleştiren ancak aynı zamanda geri çağırmayı da çağıran bir yöntem vardır:
@CallSuper
public void setSelectedItem(int position) {
if (position >= getMenu().size() || position < 0) return;
View menuItemView = getMenuItemView(position);
if (menuItemView == null) return;
MenuItemImpl itemData = ((MenuView.ItemView) menuItemView).getItemData();
itemData.setChecked(true);
boolean previousHapticFeedbackEnabled = menuItemView.isHapticFeedbackEnabled();
menuItemView.setSoundEffectsEnabled(false);
menuItemView.setHapticFeedbackEnabled(false); //avoid hearing click sounds, disable haptic and restore settings later of that view
menuItemView.performClick();
menuItemView.setHapticFeedbackEnabled(previousHapticFeedbackEnabled);
menuItemView.setSoundEffectsEnabled(true);
mLastSelection = position;
}