我有一个片段。该片段包含一个视图寻呼机,其中视图页面有多个片段。我可以滚动页面但看不到任何内容,即使getItem在滚动并初始化片段时调用了它。
我已使用support.v4.app.Fragment和support.v4.app.FragmentManager更改了代码,并将主要活动更改为FragmentActivity。另外,我使用getChildFragmentManager到适配器但仍然无法显示任何内容。
我也改变下面的行总是返回true但它可以显示第一个片段。当开始分页时,后续片段不会显示并且表现不佳。
@Override
public boolean isViewFromObject(View view, Object object) {
return true;
}
以下是我实施ViewPager的代码。
public class AppsPagerAdapter extends FragmentPagerAdapter {
private int appsSize;
private int appsPerPage = 12;
private Activity activity;
public AppsPagerAdapter (Activity activity1, FragmentManager manager, int size)
{
super(manager);
appsSize = size;
activity = activity1;
}
@Override
public int getCount() {
int offset = appsSize % appsPerPage;
int pageExtra = (offset > 0)? 1: 0;
return appsSize / appsPerPage + pageExtra;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putInt (AppPageFragment.PAGE_NUMBER_PARAMS, position);
return Fragment.instantiate(activity, AppPageFragment.class.getName(), bundle);
}
}
外片段:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate (R.layout.fragment_all_app, container, false);
ViewPager appsPager = (ViewPager) view.findViewById(R.id.grid_allapps);
//adapter = new AppsPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(), GlobalData.getInstance().getApps().getAll().size());
adapter = new AppsPagerAdapter(getActivity(), getChildFragmentManager(), GlobalData.getInstance().getApps().getAll().size());
appsPager.setAdapter(adapter);
appsPager.addOnPageChangeListener(pageChangeListener);
return view;