FragmentPagerAdapterのgetItem(int position)メソッドでは、この行PlaceholderFragment.newInstance(position + 1)が、返すフラグメントを決定します。 ありフラグメントの1種類のみで、返された断片は、同一のレイアウトを持っていますが、PlaceholderFragmentにあなたはそれが位置に応じて、レイアウト、例です変更することができます:あなたはCreating Swipe Views with Tabsをお読みください
/**
* A placeholder fragment containing a simple view.
*/
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;
}
private int mPosition;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPosition = getArguments().getInt(ARG_SECTION_NUMBER);
}
private View mRootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (mPosition) {
case 0:
mRootView = inflater.inflate(R.layout.fragment_layout_0, container, false);
break;
case 1:
mRootView = inflater.inflate(R.layout.fragment_layout_1, container, false);
break;
case 2:
mRootView = inflater.inflate(R.layout.fragment_layout_2, container, false);
break;
}
TextView textView = (TextView) mRootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return mRootView;
}
}
すべてのJavaコードはクラス内にある必要があることに注意してください。 –
申し訳ありませんが、あなたは正しいです。 –
また、これは、 'PagerAdapter'と複数の' Fragment'サブクラス(レイアウトごとに1つ)で最も適切に行われます。 –