ボトム ナビゲーション ドロワー通知に取り組んでいます。ボトム ナビゲーションの任意のアイテムをクリックして通知すると、間違ったアイテムにアイコンが表示されます。Item5 をクリックしてそこに通知を表示するメソッドを呼び出すと、Item1 に通知アイテムが表示されます。画面のスナップショットとともに、私のコードを以下に示します。私のフラグメントクラスも以下に示します。
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);
}
}
Item5 をクリックしても、通知ドロワー アイテムの Item2 に通知アイコンが表示されます。