Last active
September 10, 2022 11:02
-
-
Save Zhuinden/4b3d573a3fb538ff4937478e05fc832b to your computer and use it in GitHub Desktop.
Nested child fragments for bottom navigation view in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyFragment | |
extends Fragment { | |
public MyFragment() { | |
super(R.layout.my_fragment); | |
} | |
private int selectedIndex = 0; | |
private FirstFragment firstFragment; | |
private SecondFragment secondFragment; | |
private ThirdFragment thirdFragment; | |
private MyFragmentBinding binding = null; | |
private List<Fragment> getFragments() { | |
return new ArrayList<Fragment>() {{ | |
add(firstFragment); | |
add(secondFragment); | |
add(thirdFragment); | |
}}; | |
} | |
private List<TextView> getTabs() { | |
return new ArrayList<TextView>() {{ | |
add(binding.buttonFirstTab); | |
add(binding.buttonSecondTab); | |
add(binding.buttonThirdTab); | |
}}; | |
} | |
private FragmentTransaction selectFragment(FragmentTransaction fragmentTransaction, int selectedIndex) { | |
List<Fragment> fragments = getFragments(); | |
FragmentTransaction ft = fragmentTransaction; | |
for(int index = 0; index < fragments.size(); index++) { | |
Fragment fragment = fragments.get(index); | |
if(index == selectedIndex) { | |
ft = ft.attach(fragment); | |
} else { | |
ft = ft.detach(fragment); | |
} | |
} | |
return ft; | |
} | |
public void selectFragment(int indexToSelect) { | |
this.selectedIndex = indexToSelect; | |
setupTabSelectedState(indexToSelect); | |
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction(); | |
fragmentTransaction = selectFragment(fragmentTransaction, indexToSelect); | |
fragmentTransaction.commit(); | |
} | |
private void setupTabSelectedState(int selectedIndex) { | |
List<TextView> tabs = getTabs(); | |
for(int index = 0; index < tabs.size(); index++) { | |
TextView tab = tabs.get(index); | |
if(index == selectedIndex) { | |
tab.setTextColor(ContextCompat.getColor(requireContext(), R.color.tab_selected)); | |
} else { | |
tab.setTextColor(ContextCompat.getColor(requireContext(), R.color.tab_unselected)); | |
} | |
} | |
} | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if(savedInstanceState == null) { | |
firstFragment = new FirstFragment(); | |
secondFragment = new SecondFragment(); | |
thirdFragment = new ThirdFragment(); | |
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction() | |
.add(R.id.container, firstFragment, "firstFragment") | |
.add(R.id.container, secondFragment, "secondFragment") | |
.add(R.id.container, thirdFragment, "thirdFragment"); | |
fragmentTransaction = selectFragment(fragmentTransaction, selectedIndex); | |
fragmentTransaction.commit(); | |
} else { | |
selectedIndex = savedInstanceState.getInt("selectedIndex", 0); | |
firstFragment = (FirstFragment) getChildFragmentManager().findFragmentByTag("firstFragment"); | |
secondFragment = (SecondFragment) getChildFragmentManager().findFragmentByTag("secondFragment"); | |
thirdFragment = (ThirdFragment) getChildFragmentManager().findFragmentByTag("thirdFragment"); | |
} | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
binding = MyFragmentBinding.bind(view); | |
setupTabSelectedState(selectedIndex); | |
List<TextView> tabs = getTabs(); | |
for(int index = 0; index < tabs.size(); index++) { | |
TextView tab = tabs.get(index); | |
final int i = index; | |
tab.setOnClickListener(v -> { | |
selectFragment(i); | |
}); | |
} | |
} | |
@Override | |
public void onSaveInstanceState(@NonNull Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putInt("selectedIndex", selectedIndex); | |
} | |
@Override | |
public void onDestroyView() { | |
super.onDestroyView(); | |
binding = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment