Untitled
unknown
plain_text
9 months ago
4.8 kB
15
Indexable
public LiveData<NavController> setupWithNavController(
List<Integer> navGraphIds,
FragmentManager fragmentManager,
int containerId, Intent intent, SharedViewModel viewModel, Context context, View customTab) {
this.fragmentManager = fragmentManager;
this.containerId = containerId;
// Log.d("SavedBottomNavigationView", "Initializing fragments...");
// Initialize Fragments
fragmentMap.put(R.id.navigation_home, new HomeFragment());
fragmentMap.put(R.id.navigation_navigation, new NavigationFragment());
fragmentMap.put(R.id.nav_maintenance, new Maintenance());
fragmentMap.put(R.id.navigation_profile, new ProfileFragment());
FragmentTransaction transaction = fragmentManager.beginTransaction();
for (int i = 0; i < fragmentMap.size(); i++) {
Fragment fragment = fragmentMap.valueAt(i);
transaction.add(containerId, fragment, String.valueOf(fragmentMap.keyAt(i))).hide(fragment);
}
transaction.commit();
// Log.d("SavedBottomNavigationView", "Fragments added successfully.");
// Get logged-in user
SharedPreferences sharedPreferences = context.getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
String loggedInUser = sharedPreferences.getString("loggedInUser", "");
// Hide layout for specific users
// Set Default Fragment
activeFragment = fragmentMap.get(R.id.navigation_home);
if (activeFragment != null) {
fragmentManager.beginTransaction().show(activeFragment).commit();
// viewModel.setIsCardvisible(true);
} else {
Log.e("SavedBottomNavigationView", "HomeFragment is null!");
}
// Handle Tab Selection
setOnItemSelectedListener(item -> {
Fragment selectedFragment = fragmentMap.get(item.getItemId());
if (selectedFragment != null) {
switchFragment(selectedFragment);
if(item.getItemId() == R.id.navigation_home)
{
if(customTab != null){
customTab.setBackgroundColor(getResources().getColor(R.color.white));
}else{
customTab.setBackgroundColor(getResources().getColor(R.color.gray_light));
}
}else if(item.getItemId() == R.id.nav_maintenance){
if(customTab != null){
customTab.setBackgroundColor(getResources().getColor(R.color.white));
}else{
customTab.setBackgroundColor(getResources().getColor(R.color.gray_light));
}
}else if(item.getItemId() == R.id.navigation_navigation){
if(customTab != null){
customTab.setBackgroundColor(getResources().getColor(R.color.white));
}else{
customTab.setBackgroundColor(getResources().getColor(R.color.gray_light));
}
}else 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(getResources().getColor(R.color.white));
tabTitle.setText("Cancel");
tabIcon.animate().scaleX(1.5f).scaleY(1.5f).setDuration(300).start();
tabIcon.setImageResource(R.drawable.cancel);
}
}
else{
if(customTab != null) {
customTab.setBackgroundColor(getResources().getColor(R.color.gray_light));
ImageView tabIcon = customTab.findViewById(R.id.tab_icon);
TextView tabTitle = customTab.findViewById(R.id.tab_title);
tabIcon.setImageResource(R.drawable.profile_icon);
tabTitle.setTextColor(getResources().getColor(R.color.black_light));
tabTitle.setText("Profile");
}
}
return true;
} else {
Log.e("SavedBottomNavigationView", "Fragment not found for item ID: " + item.getItemId());
}
return false;
});
return new MutableLiveData<>();
}Editor is loading...
Leave a Comment