简体   繁体   English

为什么我的片段在viewpager中重新初始化?

[英]Why my fragments are reinitialized in my viewpager?

I have a problem with my viewpager which contains 3 Fragments : when I go to the left one, the informations of the right one disapear, and when I go to the right one, the informations of the left one disapear. 我的viewpager有一个问题,其中包含3个片段:当我转到左侧片段时,右侧片段的信息消失,而当我转到右侧片段时,左侧片段的信息消失。 By "informations" I mean textViews labels and datas from my project. “信息”是指我项目中的textViews标签和数据。

This problem causes NullPointerExceptions. 此问题导致NullPointerExceptions。

My code : 我的代码:

public class Principal extends FragmentActivity {

private PagerAdapter mPagerAdapter;
private List<Fragment> fragments;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.principal);
    DataModel.getInstance().setFragmentActivity(this);

    // Création de la liste de Fragments que fera défiler le PagerAdapter
    fragments = new Vector<Fragment>();

    // Ajout des Fragments dans la liste
    fragments.add(Fragment.instantiate(this,PageGaucheFragment.class.getName()));
    fragments.add(Fragment.instantiate(this,PageMilieuFragment.class.getName()));
    fragments.add(Fragment.instantiate(this,PageDroiteFragment.class.getName()));

    // Création de l'adapter qui s'occupera de l'affichage de la liste de Fragments
    this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
    // Affectation de l'adapter au ViewPager
    pager.setAdapter(this.mPagerAdapter);
    pager.setCurrentItem(1);

}
...



public class MyPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragments;

//On fournit à l'adapter la liste des fragments à afficher
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

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

@Override
public int getCount() {
    return this.fragments.size();
}

} }

And my fragments begin like this : 我的片段开始像这样:

public class PageGaucheFragment extends Fragment {

private TextView textViewIlluminePrecedent = null;

private View V;

private List<TextView> listeTextViewCombinaisons = new ArrayList<>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    V = inflater.inflate(R.layout.principal_page_gauche, container, false);
    ...

I looked for a solution for this problem, maybe it's a "nested" fragments problem but I don't know what it is. 我正在寻找此问题的解决方案,也许是“嵌套”碎片问题,但我不知道它是什么。

Thank for your help ! 谢谢您帮忙 !

Sorry for my english, I'm french ;) 对不起,我的英语,我是法语;)

And tell me if I didn't write enough details 告诉我我写的细节不够吗

By default a ViewPager only keeps 1 offscreen page on either side of the current page in memory at a time. 默认情况下, ViewPager只在当前页面的任一侧在内存中保留1个屏幕外页面。 This means that you have a maximum of three pages in memory, and if you are on the first or last item you only have two. 这意味着您最多可以存储三页,如果您在第一个或最后一个项目上,则只有两页。

You can adjust this by calling setOffscreenPageLimit(int) on your ViewPager. 您可以通过在ViewPager上调用setOffscreenPageLimit(int)进行调整。

However, you should do so cautiously, as this can have a serious impact on memory, especially when dealing with Fragments. 但是,您应该谨慎操作,因为这可能会对内存产生严重影响,尤其是在处理片段时。

It sounds like the real problem is that your Fragments aren't handling being recreated properly. 听起来真正的问题是您的片段无法正确地重新创建。 If you are encountering NullPointerExceptions when the ViewPager recreates your Fragments, you will likely encounter them in other situations as well. 如果在ViewPager重新创建您的片段时遇到NullPointerExceptions,则在其他情况下也可能会遇到它们。

I would spend some time looking into what is causing those NullPointerExceptions, and try to address those bugs. 我将花一些时间研究导致这些NullPointerExceptions的原因,并尝试解决这些错误。 Pay special attention to when the Fragment's lifecycle methods are called when you change ViewPager pages and that should help shed light on the source of the issue. 更改ViewPager页面时,应特别注意何时调用Fragment的生命周期方法,这应该有助于弄清问题的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM