我正在创建具有Viewpager和自定义片段类的自定义视图。问题是我没有调用getItem(int position)
。即使这是在另一个pagerview
。请告诉我我的代码有什么问题。谢谢!!
private void initView(Context context) {
this.context = context;
LayoutInflater inflater = LayoutInflater.from(context);
rootView = inflater.inflate(R.layout.internal_training_view, this, true);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
((HFTrainingActivityDashBord)context).setSupportActionBar(toolbar);
((HFTrainingActivityDashBord)context).setSupportActionBar(toolbar);
mSectionsPagerAdapterInternal = new SectionsPagerAdapter(((HFTrainingActivityDashBord)context).getSupportFragmentManager());
mViewPagerInternal = (CustomViewPager) findViewById(R.id.pager_hf_training);
mViewPagerInternal.setOffscreenPageLimit(3);
mViewPagerInternal.setAdapter(mSectionsPagerAdapterInternal);
TabLayout tabs_hf = (TabLayout) findViewById(R.id.tabs_hf);
tabs_hf.setupWithViewPager(mViewPagerInternal);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new IternalTrainingDashBoardFragment(position,context);
}
@Override
public int getCount() {
// Show 4 total pages.
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "All";
case 1:
return "InProgress";
case 2:
return "Overdue";
case 3:
return "Not Started";
}
return null;
}
}
答案 0 :(得分:2)
我认为问题在于如何在fragments
方法中返回getItem()
。使用片段中的静态newInstance()
方法创建片段。不确定是否要使用其构造函数调用片段,而不是newInstance
工厂方法这是一个示例。
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
//Note I am using the Fragments static newInstance method(). You arent supposed to call a fragment using new
return DetailInfoFragment.newInstance(someEntity);
case 1:
return DetailContentFragment.newInstance(someEntity);
case 2:
return DetailExtraFragment.newInstance(someEntity);
default:
return DetailInfoFragment.newInstance(someEntity);
}
}
以下是newInstance
类
DetailInfoFragment
public static DetailInfoFragment newInstance(SomeEntity someEntity)
{
DetailInfoFragment fragment = new DetailInfoFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_PARAM1, someEntity);
fragment.setArguments(args);
return fragment;
}