rishav rishav - 10 days ago 5
Android Question

can i seperate java class files for each pages in viewpager of tab view

I have imported the tabbed activity from android studio.
Under static placeholder fragment i have oncreate view and i have switched cases for each tab and given a seperate layout for each. I want to work in separate java class for each page in tab view. Can i seperate each cases to be in seperate class?

public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int tabNumber= getArguments().getInt(ARG_SECTION_NUMBER);
switch (tabNumber) {
case 1: {

View rootView = inflater.inflate(R.layout.tab1, container, false);


return rootView;
}
case 2: {

View rootView = inflater.inflate(R.layout.tab2, container, false);



return rootView;
}
case 3: {

View rootView = inflater.inflate(R.layout.tab3, container, false);




return rootView;
}

Answer

So rishav there is a geItem method which returns variables as super.( ..... )

just return fragment at this place.

@Override
 public Fragment getItem(int i) 
{ Fragment fragment;
 switch(i){ 
case 0: fragment = new MyFragment1(); 
break; 
case 1: fragment = new MyFragment2(); 
break; 
case 2:
 fragment = new MyFragment3(); break; 
default: 
throw new IllegalArgumentException("Invalid section number"); } 
return fragment; }
Comments