2014-02-12 52 views
1

我有中有一個viewpager片段ViewPager內

頁的一個活動中有另一個viewpager片段

的問題是,當我到達內viewpager片段的頁面,內部viewpager片段的「onCreateView」不會被調用,所以頁面是空白的。

這是爲什麼?

這裏是我的代碼:

fragment_viewpager.xml 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="0px" 
    android:layout_weight="1" > 
</android.support.v4.view.ViewPager> 

fragment_viewpager_with_logo.xml 

<include layout="@layout/logo" /> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="0px" 
    android:layout_weight="1" > 
</android.support.v4.view.ViewPager> 

public class ViewPagerFragment extends Fragment { 


private ViewPager fragmentPager; 

private PagerAdapter fragmentAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    if (container instanceof ViewPager) { 
     return inflater.inflate(R.layout.fragment_viewpager_with_logo, container, false); 
    } 
    return inflater.inflate(R.layout.fragment_viewpager, container, false); 
} 



@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
      fragmentPager = (ViewPager)getView().findViewById(R.id.pager); 
    fragmentPager.setAdapter(fragmentAdapter); 
} 

public void setAdapter(PurchaseFragmentStatePagerAdapter fragmentAdapter) { 
    this.fragmentAdapter = fragmentAdapter; 
} 

public void setCurrentItem(int i) { 
    fragmentPager.setCurrentItem(i); 
} 

public int getCurrentItem() { 
    return fragmentPager.getCurrentItem(); 
} 

public void setOnPageChangeListener(OnPageChangeListener l) { 
    if (fragmentPager == null) { 
     fragmentPager = (ViewPager) getView().findViewById(R.id.pager); 
    } 
    fragmentPager.setOnPageChangeListener(l); 
} 

}

在一些活動

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_container); 
      MyFragmentStatePagerAdapter fragmentAdapter = new MyStatePagerAdapter(getSupportFragmentManager()); 
    List<Fragment> fragments = new ArrayList<Fragment>(); 
    fragments.add(settingsFragment);// settings 
      ViewPagerFragment vpFragment = new ViewPagerFragment(); 
      fragments.add(vpFragment); 
    fragmentAdapter.setFragments(fragments); 
    fragmentPager.setAdapter(fragmentAdapter); 

      MyFragmentStatePagerAdapter innerFragmentAdapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager()); 

      List<Fragment> innerFragments = new ArrayList<Fragment>(); 
      innerFragments.add(settingsFragment); 
      innerFragmentAdapter.setFragments(innerFragments); 
      vpFragment.setAdapter(innerFragmentAdapter); 
    FragmentManager fm = getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.add(R.id.content_fragment, fragmentPager).commit(); 


} 

MyFragmentStatePagerAdapter

public class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter { 

private List<BaseFragment> fragments; 

@Inject 
public MyFragmentStatePagerAdapter(FragmentManager fm) { 
    super(fm); 
} 

public void setFragments(List<Fragment> fragments) { 
    this.fragments = fragments; 
} 

@Override 
public BaseFragment getItem(int position) { 
    return fragments.get(position); 
} 

@Override 
public int getCount() { 
    return fragments == null ? 0 : fragments.size(); 
} 

}

回答

0

在片段中使用fragment.getChildFragmentManager()在FragmentPagerAdapter的構造函數中而不是getActivity().getFragmentManager()