If I want to create a tabbed activity with more than 10 swipable fragments, what other option do I have rather than to create 10 different Fragment classes with layouts and inflate they based on their position in the tabbed layout.
That obviously sounds like a lot of duplicated code.
My question, fair and simple, is there any other way to avoid this?
You can create one fragment class and then use the int
value based on its position in the tabs to determine which layout to inflate in the onCreate
method where you usually inflate a single layout view.
Example
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(tabNumber == 1){
setContentView(R.layout.fragment_layout1);
}else if(tabNumber == 2){
setContentView(R.layout.fragment_layout2);
}else{
setContentView(R.layout.fragment_layout3);
}
}