Untitled

 avatar
unknown
plain_text
13 days ago
6.8 kB
3
Indexable
private void initNavigationView() {

        mBottomNavigation = findViewById(R.id.main__bottom_navigation);
        setupCustomTabs();
        navGraphIds = Arrays.asList(
                R.navigation.navigation_home,
                R.navigation.navigation_navigation,
                R.navigation.nav_maintenance,
                R.navigation.navigation_profile);

        mBottomNavigation.setupWithNavController(
                navGraphIds,
                getSupportFragmentManager(),
                R.id.nav_host_fragment_activity_main,
                getIntent(),viewModel, this);


//        mBottomNavigation. setOnItemSelectedListener(item -> {
//            if(item.getItemId() == R.id.navigation_profile){
//              if(customTab != null){
//                  customTab.setBackgroundColor(getResources().getColor(R.color.profile_bg));
//                  ImageView tabIcon = customTab.findViewById(R.id.tab_icon);
//                  TextView tabTitle = customTab.findViewById(R.id.tab_title);
//                  tabTitle.setTextColor(getColor(R.color.white));
//                  tabTitle.setText("Cancel");
//                tabIcon.animate().scaleX(1.5f).scaleY(1.5f).setDuration(300).start();
//                  tabIcon.setImageResource(R.drawable.cancel);
//              }
//
//            }
//           return false;
//        });
    }
    private void setupCustomTabs() {
        @SuppressLint("RestrictedApi")  BottomNavigationMenuView menuView = (BottomNavigationMenuView) mBottomNavigation.getChildAt(0);
        for (int i = 0; i < menuView.getChildCount(); i++) {
            MenuItem menuItem = mBottomNavigation.getMenu().getItem(i);
            // Inflate custom tab layout
            View customTab = LayoutInflater.from(this).inflate(R.layout.home_bottom_tab, null);

            // Optionally, set the default UI   for each tab here
            resetTabToDefault(customTab, menuItem);

            // Set click listener on the custom view
            customTab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("tg", "onClick:tb ");
                    handleTabClick(menuItem, customTab);
                }
            });

            menuItem.setActionView(customTab);
        }
    }
    private void handleTabClick(MenuItem clickedItem, View clickedView) {
        // Reset all tabs first
        for (int j = 0; j < mBottomNavigation.getMenu().size(); j++) {
            MenuItem item = mBottomNavigation.getMenu().getItem(j);
            View view = item.getActionView();
            resetTabToDefault(view, item);
        }

        int id = clickedItem.getItemId();
        // If the profile tab is clicked
        if (id == R.id.navigation_profile) {
            // If already selected, then revert and switch to home tab
            if (mBottomNavigation.getSelectedItemId() == R.id.navigation_profile) {
                resetTabToDefault(clickedView, clickedItem);
                mBottomNavigation.setSelectedItemId(R.id.navigation_home);
            } else {
                updateProfileTabSelected(clickedView);
                mBottomNavigation.setSelectedItemId(R.id.navigation_profile);
            }
        } else {
            // For other tabs, update the UI (e.g., white background) on selection
            updateTabSelected(clickedView);
            mBottomNavigation.setSelectedItemId(id);
        }
    }
    private void updateTabSelected(View view) {
        // Set a white background to indicate selection
        view.setBackgroundColor(getResources().getColor(R.color.white));
        // Optionally update icon or text color if needed
        ImageView icon = view.findViewById(R.id.tab_icon);
        TextView title = view.findViewById(R.id.tab_title);
        // For example, change tint or text color:
//        title.setTextColor(getResources().getColor(R.color.white));
    }
    private void updateProfileTabSelected(View view) {
        // For the profile tab, change icon, title, and background when selected
//        view.setBackgroundResource(R.drawable.profile_selected_background);
        view.setBackgroundColor(getResources().getColor(R.color.profile_bg));
        ImageView icon = view.findViewById(R.id.tab_icon);
        TextView title = view.findViewById(R.id.tab_title);
        title.setTextColor(getResources().getColor(R.color.white));
        title.setText("Cancel");
        icon.animate().scaleX(1.5f).scaleY(1.5f).setDuration(300).start();
        icon.setImageResource(R.drawable.cancel);
    }
    private void resetTabToDefault(View view, MenuItem menuItem) {
        // Revert the UI to the default state based on the menu item's ID
        switch (menuItem.getItemId()) {
            case R.id.navigation_home:
                view.setBackgroundColor(getResources().getColor(R.color.gray_light));
                ((ImageView)view.findViewById(R.id.tab_icon)).setImageResource(R.drawable.home2);
                ((TextView)view.findViewById(R.id.tab_title)).setText("Home");
                ((TextView)view.findViewById(R.id.tab_title)).setTextColor(getResources().getColor(R.color.black_light));
                break;
            case R.id.navigation_navigation:
                view.setBackgroundColor(getResources().getColor(R.color.gray_light));
                ((ImageView)view.findViewById(R.id.tab_icon)).setImageResource(R.drawable.navigation_module2);
                ((TextView)view.findViewById(R.id.tab_title)).setText("Navigation");
                ((TextView)view.findViewById(R.id.tab_title)).setTextColor(getResources().getColor(R.color.black_light));
                break;
            case R.id.nav_maintenance:
                view.setBackgroundColor(getResources().getColor(R.color.gray_light));
                ((ImageView)view.findViewById(R.id.tab_icon)).setImageResource(R.drawable.incident_module);
                ((TextView)view.findViewById(R.id.tab_title)).setText("Maintenance");
                ((TextView)view.findViewById(R.id.tab_title)).setTextColor(getResources().getColor(R.color.black_light));
                break;

            case R.id.navigation_profile:
                view.setBackgroundColor(getResources().getColor(R.color.gray_light));
                ((ImageView)view.findViewById(R.id.tab_icon)).setImageResource(R.drawable.profile_icon);
                ((TextView)view.findViewById(R.id.tab_title)).setText("Profile");
                ((TextView)view.findViewById(R.id.tab_title)).setTextColor(getResources().getColor(R.color.black_light));
                break;
        }
    }
Editor is loading...
Leave a Comment