嵌套的Viewpager Fragment未初始化

时间:2016-07-06 06:33:17

标签: android android-fragments android-viewpager fragmentpageradapter android-navigation-drawer

我有3个fragments part of a navigation drawer,其中一个包含一个viewpager。当我第一次加载应用程序时,viewpager会加载所有片段。但是,当我使用navi-drawer切换到另一个片段并切换回时,其中一个viewpager片段未加载。我尝试过代码herehere,但我无法提出解决方案。

在我的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;
}

1 个答案:

答案 0 :(得分:3)

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();