我已阅读了几篇文章(http://tinyurl.com/pb8es74,http://tinyurl.com/p9pcfcv..。),但未找到解决方案。
我有一个MainActivity,它加载一个包含ViewPager的布局,其中包含三个片段(根据方向有不同的布局)。
我已将此功能添加到活动中,因此当方向更改时它不会被销毁,因为我可能会打开一些对话框或弹出窗口:
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。但是我需要清单中的那一行。
答案 0 :(得分:0)
最后我用DialogFragments做了,因为我需要继续。 无论如何,如果有人知道如何以我在未来发展的问题中解释的方式解决问题,那就太棒了。
由于