作成したばかりのアクティビティで、内部クラスを見つけることができますSectionsPagerAdapter
。この方法を見てください:
@Override
public Fragment getItem(int i) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
すべてのタブのこのメソッドは、異なるバンドルのみを持つ DummySectionFragment のインスタンスを返します。タブごとに異なるビューでフラグメントを作成する場合は、i
変数の値を確認し、この値に従って適切なフラグメントを作成する必要があります。例えば:
@Override
public Fragment getItem(int i) {
Fragment fragment;
switch(i){
case 0:
fragment = new MyFragment1();
break;
case 1:
fragment = new MyFragment2();
break;
case 3:
fragment = new MyFragment3();
break;
default:
throw new IllegalArgumentException("Invalid section number");
}
//set args if necessary
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
クラスの代わりにDummySectionFragment
、MyFragment1、MyFragment2、MyFragment2 の 3 つのクラスを作成し、それぞれについて、メソッド内でonCreateView
ビューを作成またはインフレートします。次に例を示します。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_fragment1.xml, null);
return v;
}
R.layout.my_fragment1.xml は MyFragment1 フラグメントのレイアウトです。