public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show image
return ImageFragment.init(position);
case 1: // Fragment # 1 - This will show image
return ImageFragment.init(position);
default:// Fragment # 2-9 - Will show list
return ArrayListFragment.init(position);
}
}
}
}
public class ImageFragment extends Fragment {
int fragVal;
static ImageFragment init(int val) {
ImageFragment truitonFrag = new ImageFragment();
// Supply val input as an argument.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.fragment_image, container,
false);
View tv = layoutView.findViewById(R.id.text);
((TextView) tv).setText("Truiton Fragment #" + fragVal);
return layoutView;
}
}
public class ArrayListFragment extends ListFragment {
int fragNum;
String arr[] = {"1", "2", "2", "3", "4", "5"};
static ArrayListFragment init(int val) {
ArrayListFragment truitonList = new ArrayListFragment();
// Supply val input as an argument.
Bundle args = new Bundle();
args.putInt("val", val);
truitonList.setArguments(args);
return truitonList;
}
/** * Retrieving this instance's number from its arguments. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
}
/** * The Fragment's UI is a simple text view showing its instance number and
* an associated list. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.fragment_pager_list,
container, false);
View tv = layoutView.findViewById(R.id.text);
((TextView) tv).setText("Truiton Fragment #" + fragNum);
return layoutView;
}
@Override
No comments:
Post a Comment