IllegalStateException: Can't change tag of fragment was android:switcher now android:switcher

后端 未结 1 1651
挽巷
挽巷 2021-02-13 03:19

My activity uses TabLayout + ViewPager.

The number of tabs and pages here are dynamic depending on the data fetch from the server.

Th

1条回答
  •  再見小時候
    2021-02-13 04:09

    The FragmentPagerAdapter already caches the Fragments for you. Each fragment is assigned a tag, and then the FragmentPagerAdapter tries to call findFragmentByTag. It only calls getItem() if the result from findFragmentByTag is null.

    You're probably getting this error because you're adding the same fragment instance to the list. You should create a new instance for each page.

    Example form document :

    //...
          public static class MyAdapter extends FragmentPagerAdapter {
                public MyAdapter(FragmentManager fm) {
                    super(fm);
                }
    
                @Override
                public int getCount() {
                    return NUM_ITEMS;
                }
    
                @Override
                public Fragment getItem(int position) {
                    return ArrayListFragment.newInstance(position);// IMPORTANT
                }
            }
        //..
    

    Refer :FragmentPagerAdapter

    Main thread : Retrieve a Fragment from a ViewPager

    0 讨论(0)
提交回复
热议问题