我在我的活动中使用ViewPager,它只有两个片段,两者都以类似的结构实现,因此在调用getInstanced时应该总是返回一个片段:
public class ListReminderFragment extends Fragment {
private View rootView;
private static ListReminderFragment listReminderFragment;
public static ListReminderFragment getInstance() {
if(listReminderFragment != null)
return listReminderFragment;
else {
listReminderFragment = new ListReminderFragment();
return listReminderFragment;
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.content_main_screen, container, true);
return rootView;
}
@Nullable
@Override
public View getView() {
return rootView;
}
}
这是我的适配器:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ListReminderFragment.getInstance(); // temporary
}
@Override
public int getCount() {
return 2;
}
}
我正在使用ViewPager的活动中的OnCreate()初始化我的ViewPager,如下所示:
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
ViewPager viewPager = (ViewPager) findViewById(R.id.main_view_pager);
viewPager.setAdapter(viewPagerAdapter);
但是当我调用viewPager.getChildAt(),viewPagerAdapter.getItem(0).getView()时,它们都返回null,
当我调用viewPager.getChildCount()时,它返回0而不是2.
如何访问ViewPager中片段旁边的视图?