Android FragmentStatePagerAdapter

后端 未结 5 1339
太阳男子
太阳男子 2021-02-01 18:02

I\'m working with a FragmentStatePagerAdapter using this example.

The MyAdapter class is implemented as follows:

public static class MyAdapter extends Fr         


        
5条回答
  •  情话喂你
    2021-02-01 18:39

    I think the solution is much simpler than the answers provided.

    If you take a look at the source of FragmentStatePagerAdapter.instantiateItem(), you'll notice that instantiateItem() handles this logic for you, and thus your implementation of getItem() should always return a new instance.

    So in order to return an existing Fragment, simply do (assuming you're calling from ViewPager):

    Fragment f = (Fragment) getAdapter().instantiateItem(this, getCurrentItem());
    

提交回复
热议问题