ViewPager first fragment shown is always wrong with FragmentStatePager

拜拜、爱过 提交于 2019-12-05 03:17:36

1) Do not implement getItemPosition(Object) if you don't handle it. You're not required to implement it and you might break some other functionality by implementing it wrong.

2) The point of getItem(int) is to return a new fragment. Ditch the fragment array as it makes no sense.

3) Make the adpater class static (It promotes reusability, the adapter should not depend on the parent class to get its data set, right?) and pass the Categorys as a constructor parameter. Store it in a variable and make new fragments according to this data set. You'll also probably want to pass a Category[position] as a parameter to the fragment constructor instead of just position.

The getItem() implementation is the problem.

@Override
public Fragment getItem(final int position) {
    return FakeFragment.newInstance(position);
}

You should never alter the data in this get method: do not call add() in it. I doubt the Adapter would know at this point that you added an element.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!