ViewPager和TabLayout中的警告(未更新内联;预期状态3找到2)

时间:2016-06-15 08:01:30

标签: android android-fragments android-viewpager fragmentmanager

  

compile' com.android.support:support-v4:23.4.0'使用此版本的支持库。

通过此链接但无法找到确切的解决方案

  

Tab+ViewPager not updating instead shows weird warning expected state 3 found 2

     

Android ViewPager Loading incorrect fragments

W / FragmentManager:moveToState:A {7bb406c#3 id = 0x7f0d0087}的片段状态未内联更新;预期状态3发现2

  

活动中的TabLayout和ViewPager。

if (tabLayout!=null) {
        tabLayout.addTab(tabLayout.newTab().setText("A"));
        tabLayout.addTab(tabLayout.newTab().setText("B"));
        tabLayout.addTab(tabLayout.newTab().setText("C"));
        tabLayout.addTab(tabLayout.newTab().setText("D"));
        tabLayout.addTab(tabLayout.newTab().setText("E"));
        tabLayout.addTab(tabLayout.newTab().setText("F"));
        tabLayout.addTab(tabLayout.newTab().setText("G"));
        tabLayout.addTab(tabLayout.newTab().setText("H"));
    }

    adapter=new ViewPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    mVPCompare.setAdapter(adapter);


    mVPCompare.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mVPCompare.setCurrentItem(tab.getPosition(),true);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
  

适配器类。

public class ViewPagerAdapter extends FragmentStatePagerAdapter {


int pages;

public ViewPagerAdapter(FragmentManager fm,int pages) {
    super(fm);
    this.pages=pages;
}
@Override
public Fragment getItem(int position) {



    Fragment fragment;

    switch (position)
    {
        case 0:
            fragment=new A();
            return fragment;

        case 1:
            fragment=new B();
            return  fragment;

        case 2:
            fragment=new C();
            return fragment;

        case 3:
            fragment=new D();
            return fragment;

        case 4:
            fragment=new E();
            return fragment;

        case 5:
            fragment=new F();
            return fragment;

        case 6:
            fragment=new G();
            return fragment;

        case 7:
            fragment=new H();
            return fragment;

        default:
            return null;
    }

}

@Override
public int getCount() {
    return pages;
}

}

0 个答案:

没有答案