android – ViewPager里面的片段,怎么保留状态?

前端之家收集整理的这篇文章主要介绍了android – ViewPager里面的片段,怎么保留状态?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,片段活动包含片段A和片段B两个片段.片段B是包含3个片段的视图寻呼机.

在我的活动中,为了防止在配置更改中重新生成片段:

if(getSupportFragmentManager().findFragmentByTag(MAIN_TAB_FRAGMENT) == null) {
    getSupportFragmentManager().beginTransaction().replace(R.id.container,new MainTabFragment(),MAIN_TAB_FRAGMENT).commit();
}

片段代码B:

public class MainTabFragment extends Fragment {

    private PagerSlidingTabStrip mSlidingTabLayout;
    private LfPagerAdapter adapter;
    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab,container,false);
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {

        this.adapter = new LfPagerAdapter(getChildFragmentManager());

        this.mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        this.mViewPager.setAdapter(adapter);

        this.mSlidingTabLayout = (PagerSlidingTabStrip) view.findViewById(R.id.sliding_tabs);
        this.mSlidingTabLayout.setViewPager(this.mViewPager);
    }
}

适配器代码

public class LfPagerAdapter extends FragmentPagerAdapter {

    private static final int NUM_ITEMS = 3;

    private FragmentManager fragmentManager;

    public LfPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragmentManager = fm;
    }

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

    @Override
    public Fragment getItem(int position) {
        Log.d("TEST","TEST");
        switch (position) {
            case 1:
                return FragmentC.newInstance();
            case 2:
                return FragmentD.newInstance();
            default:
                return FragmentE.newInstance();
        }
    }
}

我的问题是,我无法保留视图寻呼机的状态,它的小孩片段的方向更改.

显然,这是每个轮回的要求:

this.adapter = new LfPagerAdapter(getChildFragmentManager());

这将导致整个寻呼机被重新创建,对吧?结果是

getItem(int position)

将会在每个轮播中被调用,片段将从头创建并丢失其状态:

return FragmentC.newInstance();

我试着解决这个问题:

if(this.adapter == null)
    this.adapter = new LfPagerAdapter(getChildFragmentManager());

在onViewCreated中,但是结果是旋转了被移除的寻呼机内的碎片.

任何想法如何正确保留传呼机内的状态?

解决方法

您需要做两件事来解决问题:

1)您应该使用onCreate方法代替onViewCreated来实例化LfPagerAdapter;

即:

public class MainTabFragment extends Fragment {

    private PagerSlidingTabStrip mSlidingTabLayout;
    private LfPagerAdapter adapter;
    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        this.adapter = new LfPagerAdapter(getChildFragmentManager());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {


        this.mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        this.mViewPager.setAdapter(adapter);

        this.mSlidingTabLayout = (PagerSlidingTabStrip) view.findViewById(R.id.sliding_tabs);
        this.mSlidingTabLayout.setViewPager(this.mViewPager);
    }
}

2)您将需要扩展FragmentStatePagerAdapter而不是FragmentPagerAdapter

原文链接:https://www.f2er.com/android/311440.html

猜你在找的Android相关文章