嵌套的 Viewpager 片段未初始化

Nested Viewpager Fragment not getting initialized

我有 3 个 fragments part of a navigation drawer,其中一个包含一个 viewpager。当我第一次加载应用程序时,viewpager 会加载所有片段。但是,当我使用导航抽屉切换到另一个片段并切换回来时,其中一个 viewpager 片段没有加载。我试过代码 here and here 但我无法想出解决方案。

在我的 pageradapter 代码中,在不同片段的 switch case 中;我已经为该位置放置了一个日志,并且在应用程序初始加载后位置 1 似乎没有出现。

我的问题是,为什么位置 1 从来没有被跟注过?有人可以给我解释一下吗?

这是我的 Pageradapter 代码:

 class PagerAdapter extends FragmentStatePagerAdapter {

static HeartRate_Fragment heartRate_fragment;
static Calorie_Fragment calorie_fragment;
static  Steps_Fragment steps_fragment;

int num;
public PagerAdapter(Context context, FragmentManager fm) {
    super(fm);

}
@Override
public Fragment getItem(int position) {
    //add new fragment here for homepage viewpager
    Log.d("position",position+"");
    switch (position) {

        case 0:
           if(calorie_fragment == null)
            calorie_fragment = new Calorie_Fragment();
           Log.d("flip",0+"");
            return calorie_fragment;

        case 1:
            if(heartRate_fragment == null)
                heartRate_fragment = new HeartRate_Fragment();
            Log.d("flip",1+"");
            return heartRate_fragment;


        case 2:
            if(steps_fragment == null)
            steps_fragment = new Steps_Fragment();
            Log.d("flip",2+"");
            return steps_fragment;


        default:
            return null;

    }

}

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

public HeartRate_Fragment getHRFragment()
{
    return heartRate_fragment;
}
public Calorie_Fragment getCalorie_fragment()
{
    return calorie_fragment;
}
public  Steps_Fragment getSteps_fragment()
{
    return steps_fragment;
}
class PagerAdapter extends FragmentStatePagerAdapter {

static HeartRate_Fragment heartRate_fragment;
static Calorie_Fragment calorie_fragment;
static  Steps_Fragment steps_fragment;

int num;
public PagerAdapter(Context context, FragmentManager fm) {
super(fm);

}
@Override
public Fragment getItem(int position) {
  //add new fragment here for homepage viewpager
Log.d("position",position+"");
switch (position) {

    case 0:

        if(calorie_fragment == null){
        calorie_fragment = new Calorie_Fragment();
        return calorie_fragment;
        }else {
         return getCalorie_fragment(); }         

    case 1:
        if(heartRate_fragment == null){
            heartRate_fragment = new HeartRate_Fragment();
            return heartRate_fragment;}
         else{ return getHRFragment();}

    case 2:
        if(steps_fragment == null){
        steps_fragment = new Steps_Fragment();
        return steps_fragment;
        }
        else{ return getSteps_fragment();} 

    default:
        return null;

}

}

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

 public HeartRate_Fragment getHRFragment()
  {
  return heartRate_fragment;
  }
  public Calorie_Fragment getCalorie_fragment()
  {
  return calorie_fragment;
  }
  public  Steps_Fragment getSteps_fragment()
  {
  return steps_fragment;
  }

尝试此操作以确保您使用的是 getChildFragmentManager();