【问题标题】:SlidingTabLayout with Fragments and ViewPager带有 Fragments 和 ViewPager 的 SlidingTabLayout
【发布时间】:2015-01-12 13:37:27
【问题描述】:

我想使用 SlidingTabLayout 并让内容成为片段的内容。

我试过了,但显然是错误的。

    @Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment fragment = null;
    if(position == 0){
        fragment = new Fragment1();
    }
    else if(position == 1) {
        fragment = new Fragment2();
    return fragment;
}

这样做的最佳做法是什么? 如果有影响,我的片段中包含列表

【问题讨论】:

    标签: android tabs android-viewpager fragment sliding


    【解决方案1】:

    我会在 FragmentPagerAdapter 中创建一个片段数组,以便您可以在 instantiateItem 回调中执行类似的操作:

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    if (mFragments[position] == null) { //mFragments is the array of Fragments
        mFragments[position] = new Fragment();
    }
    return mFragments[position];
    }
    

    这样你就不会在每次切换标签时返回一个新的片段

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多