[英]Bottom navigation drawer showing wrong icon on items
我正在處理“底部導航”抽屜通知,當我單擊“底部導航”的任何項目進行通知時,它在錯誤的項目上顯示圖標。 可以說,如果我單擊Item5並調用方法在此處顯示Notification,那么它將在Item1上顯示Notification項目。 下面給出了我的代碼以及屏幕快照。 我的片段類也如下所示:
MainActivity類:
AHBottomNavigation bottomNavigation;
Fragment selectedFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigation = (AHBottomNavigation) findViewById(R.id.navigation);
AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.home, R.drawable.home, R.color.colorAccent);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.menu, R.drawable.menu, R.color.colorAccent);
AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.cart, R.drawable.cart, R.color.colorAccent);
AHBottomNavigationItem item4 = new AHBottomNavigationItem(R.string.orders, R.drawable.orders, R.color.colorAccent);
AHBottomNavigationItem item5 = new AHBottomNavigationItem(R.string.settings, R.drawable.setting, R.color.colorAccent);
bottomNavigation.addItem(item1);
bottomNavigation.addItem(item2);
bottomNavigation.addItem(item3);
bottomNavigation.addItem(item4);
bottomNavigation.addItem(item5);
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
bottomNavigation.setAccentColor(Color.parseColor("#571e19"));
// selectedFragment = ItemHomeFragment.newInstance(bottomNavigation);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
if (position == 0) {
selectedFragment = ItemHomeFragment.newInstance(bottomNavigation);
} else if (position == 1) {
selectedFragment = ItemMenuFragment.newInstance(bottomNavigation);
} else if (position == 2) {
selectedFragment = ItemCardFragment.newInstance(bottomNavigation);
} else if (position == 3) {
selectedFragment = ItemOrdersFragment.newInstance(bottomNavigation);
} else if (position == 4) {
selectedFragment = ItemSettingsFragment.newInstance(bottomNavigation);
}
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout,selectedFragment);
fragmentTransaction.commit();
return true;
}
});
}
@Override
protected void onStart() {
super.onStart();
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, ItemHomeFragment.newInstance(bottomNavigation));
fragmentTransaction.commit();
}
我的片段類:
public class ItemSettingsFragment extends Fragment {
public static AHBottomNavigation bottomNavigation1;
public static ItemSettingsFragment newInstance(AHBottomNavigation bottomNavigation) {
ItemSettingsFragment fragment = new ItemSettingsFragment();
// Initializing Navigation Drawer
bottomNavigation1 = bottomNavigation;
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
setNotification();
return view;
}
// Calling this method in my 5 Item of Notification Drawer, but '1' is showing in first item of Bottom Noficiation drawer
public static void setNotification(){
bottomNavigation1.setNotification("1", 1);
}
}
單擊項目5,但通知圖標出現在我的通知抽屜項目的項目2中。
我已經檢查了您的代碼,並嘗試將其與AHBottomNavigation的 github repo進行比較 ,您猜怎么着? 我發現了這個小錯誤。
public static void setNotification(){
bottomNavigation1.setNotification("1", 1);
}
現在檢查這一行,第一個參數是您的通知計數器,第二個參數是您的標簽號,您傳遞的是“ 1”,這意味着在“ 1”標簽上設置了通知標志,並且由於標簽計數器以零開頭,因此在第二個標簽。
答案很簡單。 只是用這個改變它
public static void setNotification(){
bottomNavigation1.setNotification("1", 4);
}
當您這樣做時:
public static void setNotification(){
bottomNavigation1.setNotification("1", 1);
}
“ 1”是通知
1是項目位置
因此,您要在位置1的項目處設置通知:“菜單”
您可以做什么:
以這種方式修改方法:
public static void setNotification(String notification, int itemPostion){
bottomNavigation1.setNotification(motification, itemPostion);
}
然后,當您調用方法時:
setNotification("Any message or number", yourItemPostion)
示例:在設置中將“ 2”設置為:
setNotification("2", 4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.