我有創造的片段這樣的適配器:片段實例,onCreate方法不調用
public class FooAdapter<T extends Fragment> extends FragmentPagerAdapter {
private Class<T> c;
public FooAdapter(FragmentManager fm,Class<T> c) {
super(fm);
this.c = c;
}
@Override
public Fragment getItem(int position) {
try {
T fragment = c.newInstance();
return fragment;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
適配器的實例:
new FooAdapter<BarFragment>(getSupportFragmentManager(),BarFragment.class)
我把日誌中的onCreate(和onAttach,onCreateView)的BarFragment,但沒有記錄!
public class BarFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "on create view");
}
}
您是否將適配器分配給viewpager? – zec
@zec是我已分配。 –
'getItem()'和'getCount()'都需要被覆蓋。那麼你打算每個片段只有一個適配器/ viewpager? – zec