我找到了一个使用片段而不使用tabBar的示例 但现在我发现一个片段OnCreatView调用了previos片段,并且我对片段的生命周期没有任何控制
这是我的代码
public class AddTextActivity AppCompatActivity {
private PagerAdapter mPagerAdapter;
public static FragmentManager frmanager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cordinate);
frmanager=getSupportFragmentManager();
initialisePaging();
}
private void initialisePaging() {
List<Fragment> fragments = new Vector<>();
fragments.add(Fragment.instantiate(this,Frags.Addtext1.class.getName()));
fragments.add(Fragment.instantiate(this,Frags.Addtext2.class.getName()));
fragments.add(Fragment.instantiate(this,Frags.Addtext3.class.getName()));
this.mPagerAdapter = new PagerAdapter1(frmanager, fragments);
ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
}
这是pagerAdapter1
public class PagerAdapter1 extends FragmentPagerAdapter {
private List<Fragment> fragments;
/**
* @param fm
* @param fragments
*/
public PagerAdapter1(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
}
所以现在我怎么能阻止调用下一个片段的onCreatView?
答案 0 :(得分:0)
确定选择了哪个页面:
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// TODO insert your code here
}
@Override
public void onPageScrollStateChanged(int state) {
}
});