简体   繁体   English

当方向改变时,ViewPager中的碎片消失

[英]Fragments inside ViewPager dissapear when orientation changes

I have read several posts ( http://tinyurl.com/pb8es74 , http://tinyurl.com/p9pcfcv.. .) about this but cannot find a solution. 我已经阅读了几篇关于此的帖子( http://tinyurl.com/pb8es74,http://tinyurl.com/p9pcfcv ... ),但无法找到解决方案。

I have a MainActivity which loads a layout containing a ViewPager with three fragments (which have different layouts depending on orientation). 我有一个MainActivity,它加载一个包含ViewPager的布局,其中包含三个片段(根据方向有不同的布局)。

I have added this feature to the activity so it is not destroyed when the orientation changes as I might have some dialogs or popips opened: 我已将此功能添加到活动中,因此当方向更改时它不会被销毁,因为我可能会打开一些对话框或弹出窗口:

android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|uiMode"

Therefore, I have overwritten the method "onConfigurationChanged" which just calls the method loadLayout, where everything regarding the view is made: 因此,我已经覆盖了方法“onConfigurationChanged”,它只调用方法loadLayout,其中有关视图的所有内容:

 private void loadLayout() {
    setContentView(R.layout.layout_main_activity);

    pager = (ViewPager) this.findViewById(R.id.pager);
    pager.setPageTransformer(true, new DepthPageTransformer());

    adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(Fragment1.getInstance());
    adapter.addFragment(Fragment2.getInstance());
    adapter.addFragment(Fragment3.getInstance());

    pager.setAdapter(adapter);
    pager.setOffscreenPageLimit(2);

    //Media controllers
    imgMediaController= (ImageButton) findViewById(R.id.media_controller);

    imgMediaController.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            [...]
        }
    });

    imgVolumeController= (ImageButton) findViewById(R.id.volume_controller);
    gM.setImgVolumeController(imgVolumeController);

    imgVolumeController.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            [...]
        }
    });
}

The code for the fragments is equal: 片段的代码是相同的:

public class FragmentNature extends Fragment {

private static FragmentNature instance;
private SoundManager sM;
private View rootView;
Hashtable<String, ImageButton> h_Images;
private Handler repeatUpdateHandler = new Handler();
private Toast volumeToast;


public static FragmentNature getInstance() {
    if (instance == null)
        instance = new FragmentNature();
    return instance;
}

public void nullInstance() {
    instance=null;
}

public FragmentNature(){
    h_Images=new Hashtable<String,ImageButton>();
}

public void onCreate(Bundle savedInstanceState) {
    Log.d("Naturing", "On create->Estoy en el fragment nature");
    super.onActivityCreated(savedInstanceState);
    sM=SoundManager.getInstance();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    rootView=null;
    Log.d("Naturing", "On createView->Estoy en el fragment nature");
    rootView = inflater.inflate(R.layout.fragment_nature, container, false);

    final ImageButton imgRain= (ImageButton) rootView.findViewById(R.id.n_rain);
    h_Images.put("n_rain",imgRain);
    imgRain.setImageResource(R.drawable.n_rainsel);

    final ImageButton imgRiver= (ImageButton) rootView.findViewById(R.id.n_river);
    imgRiver.setImageResource(R.drawable.n_riversel);

    final ImageButton imgWave= (ImageButton) rootView.findViewById(R.id.n_wave);
    h_Images.put("n_wave",imgWave);
    imgWave.setImageResource(R.drawable.n_wavesel);

    final ImageButton imgWind= (ImageButton) rootView.findViewById(R.id.n_wind);
    h_Images.put("n_wind", imgWind);
    imgWind.setImageResource(R.drawable.n_windsel);

    final ImageButton imgFire= (ImageButton) rootView.findViewById(R.id.n_fire);
    h_Images.put("n_fire", imgFire);
    imgFire.setImageResource(R.drawable.n_firesel);

    final ImageButton imgTree= (ImageButton) rootView.findViewById(R.id.n_tree);
    h_Images.put("n_tree", imgTree);
    imgTree.setImageResource(R.drawable.n_treesel);

    final ImageButton imgStorm= (ImageButton) rootView.findViewById(R.id.n_storm);
    h_Images.put("n_storm", imgStorm);
    imgStorm.setImageResource(R.drawable.n_stormsel);

    final ImageButton imgDrop= (ImageButton) rootView.findViewById(R.id.n_drop);
    h_Images.put("n_drop", imgDrop);
    imgDrop.setImageResource(R.drawable.n_dropsel);

    final ImageButton imgDeepSea= (ImageButton) rootView.findViewById(R.id.n_deepsea);
    h_Images.put("n_deepsea", imgDeepSea);
    imgDeepSea.setImageResource(R.drawable.n_deepseasel);

    return rootView;
}

private boolean isNetworkAvailable(){
    Context context=ApplicationManager.getAppContext();
    ConnectivityManager connectivityManager=
            (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo= connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

public void changeImageStatus(String sound, int imgId){
    Log.d("Naturing", "FragmentNature::changeImageStatus  | sound= " + sound + "  imgId=" + imgId);
    h_Images.get(sound).setImageResource(imgId);
}

} }

The problem is that when the orientation changes, the mainActivity layout is correctly loaded, but the viewPager is not. 问题是当方向改变时,mainActivity布局被正确加载,但viewPager不是。 In fact it dissapears and nothing can be seen in its place. 事实上,它消失了,在它的位置上看不到任何东西。 Anyway, the viewPager is there as I am able to go through it and reach the end (I know this because I can see the blue sign when you reach the end of a menu and so). 无论如何,viewPager在那里,因为我能够通过它并到达终点(我知道这是因为当你到达菜单的末尾时我可以看到蓝色标志等等)。

I have also tried to call the OncreateView method of the fragments but nothing changes. 我也尝试调用片段的OncreateView方法但没有任何变化。

Any idea? 任何想法? Thanks and kind regards 谢谢和亲切的问候

EDIT 1 编辑1

If I take out "pager.setOffscreenPageLimit(2);", after orientation changes, fragment 1 and fragment 3 are visible when I scroll the viewpager for a while, but fragment 2 does not appear in any case. 如果我取出“pager.setOffscreenPageLimit(2);”,在方向更改后,当我滚动viewpager一段时间时,片段1和片段3是可见的,但片段2在任何情况下都不会出现。 I know it is not much but in case it gives a hint to anyone. 我知道它并不多,但万一它会暗示任何人。

EDIT 2 编辑2

If I take out the line in the Manifest, it works ok as it calls the onCreateView of the MainActivity and Fragment. 如果我取出Manifest中的行,它可以正常工作,因为它调用MainActivity和Fragment的onCreateView。 But I need the line in the manifest. 但是我需要清单中的那一行。

In the end I did it with DialogFragments, because I needed to go ahead. 最后我用DialogFragments做了,因为我需要继续。 Anyway, it would be great if anyone knows anything about how to overcome the problem in the way I explained in the question for future developments. 无论如何,如果有人知道如何以我在未来发展的问题中解释的方式解决问题,那就太棒了。

Thanks 谢谢

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

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