这个问题类似于many other questions,它描述了我正在经历的相同问题:我有一个FragmentPagerAdapter,它有5个条目,在滑动到第三个选项卡/向后滑动后不显示片段。然而,在我的例子中,我的ViewPager不是托管在片段中,而是直接保存在活动中,因此getChildFragmentManager()解决方案显然不适用于这里。
我的活动
public class NotificationListActivity extends AppCompatActivity {
@BindView(R.id.container)
ViewPager mViewPager;
@BindView(R.id.tabs)
TabLayout tabLayout;
@BindView(R.id.toolbar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_list);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
mViewPager.setAdapter(new CategoryPagerAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(mViewPager);
}
...我的适配器
public class CategoryPagerAdapter extends FragmentPagerAdapter {
@NonNull
private final String[] pageTitles;
@NonNull
private final Fragment[] fragments;
public CategoryPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
this.pageTitles = // Intialization ommitted for brevity
this.fragments = // Initialization ommitted for brevity
}
@Override
public Fragment getItem(int position) {
if (position > fragments.length - 1)
throw new IllegalArgumentException("<ommitted>");
return fragments[position];
}
@Override
public int getCount() {
return pageTitles.length;
}
@Override
public CharSequence getPageTitle(int position) {
if (position > pageTitles.length - 1)
throw new IllegalArgumentException("<ommitted>");
return pageTitles[position];
}发布于 2016-07-04 10:51:48
我通过增加OffscreenPageLimit来解决这个问题:
mViewPager.setOffscreenPageLimit(5);(我有5个标签)
这并不优雅,但目前解决了这个问题。我不会接受这个答案,因为这不可能是正确的方法。
https://stackoverflow.com/questions/38147599
复制相似问题