我有一个MainActivity,它加载一个包含VIEwPager的布局,其中包含三个片段(根据方向有不同的布局).
我已将此功能添加到活动中,因此当方向更改时它不会被销毁,因为我可能会打开一些对话框或d出窗口:
androID:configChanges="keyboardHIDden|orIEntation|screenSize|screenLayout|uiMode"
因此,我已经覆盖了方法“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) { [...] } });}
片段的代码是相同的:
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);}
}
问题是当方向改变时,mainActivity布局被正确加载,但vIEwPager不是.事实上,它消失了,在它的位置上看不到任何东西.无论如何,vIEwPager在那里,因为我能够通过它并到达终点(我知道这是因为当你到达菜单的末尾时我可以看到蓝色标志等等).
我也尝试调用片段的OncreateVIEw方法但没有任何变化.
任何的想法?
谢谢和亲切的问候
编辑1
如果我取出“pager.setoffscreenPagelimit(2);”,在方向更改后,当我滚动vIEwpager一段时间时,片段1和片段3是可见的,但片段2在任何情况下都不会出现.我知道它并不多,但万一它会暗示任何人.
编辑2
如果我取出Manifest中的行,它可以正常工作,因为它调用MainActivity和Fragment的onCreateVIEw.但是我需要清单中的那一行.
解决方法 最后我用DialogFragments做了,因为我需要继续.无论如何,如果有人知道如何以我在未来发展的问题中解释的方式解决问题,那就太棒了.
谢谢
总结以上是内存溢出为你收集整理的android – 当方向改变时,ViewPager内部的碎片消失全部内容,希望文章能够帮你解决android – 当方向改变时,ViewPager内部的碎片消失所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)