Android Fragment Inflate layout

点点圈 提交于 2019-12-14 03:25:47

问题


Hi I'm working with a view pager and fragments. I would like to override orientation changes so that the onCreateView method isn't run everytime the orientation changes. This is because a lot is run in onCreateView for my app which causes the orientation to be slow to change. Also things on my server are checked and what not too so there really is no reason for all of it to run just because the orientation changed.

Here is what i've set up so far

I have this on the MainActivity in the manifest

 android:configChanges="orientation|keyboardHidden"

And then i am using this in my fragment

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
    getLayoutInflater(getArguments()).inflate(R.layout.main, null, false);
    Toast.makeText(getActivity(), "changed orientation", Toast.LENGTH_LONG).show();
}

When i run the app and switch orientation i see the toast show but the layout never changes to the landscape one. So I decided that maybe android just wasn't recognizing that it was in landscape since i overrided it but changing the layout to something ridiculous like a layout for a dialog doesn't cause a force close or anything it stays the portrait layout which is stretched as a result so i know there is something wrong with how i'm inflating the layout.

Any help on changing the layout when orientation is changed in a viewpager would be greatly appreciated.

Thank you!


回答1:


1) You should put all your server checking routines and other one-time operations in Fragment onCreate(), not onCreateView(). If you need to show some kind of "Loading..." screen while it loads, then I would recommend using a FragmentActivity to inflate a layout with at least a FrameLayout that contains a "Loading..." type of graphic. First, the FragmentActivity will run onCreate(), in which it will inflate the initial View first and then instantiate the Fragment. Put the server checking code in the Fragment's onCreate(), and then inflate any View for this Fragment in the its onCreateView() method. Here's a sample from one of my projects that initializes a Microphone button, which is really a fragment:

<FrameLayout
       android:id="@+id/recorder_mic_fragment"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       />

Then, the microphone fragment is actually instantiated by the following Java code:

// make the Mic button
FragmentManager fragMgr = getFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();
if (null == fragMgr.findFragmentByTag(FRAG_TAG)) {
    xact.add(R.id.mic_button_frame, MicFragment.newInstance(queryType), FRAG_TAG).commit();
}

FRAG_TAG is actually a unique tag name you give to a fragment instance that's different from the R.id of the fragment. This way, you can use findFragmentByTag() to make sure fragments get instantiated correctly in all the places you need them to.

You can massage this code a little bit if you're looking to load Fragments within Fragments, like how my application actually works. If you have a Fragment that will inflate one of several possible Views, then use an if statement to pick the appropriate frame ID and designate a unique FRAG_TAG. I would advise using distinct R.id's for each FrameLayout you create for a Fragment. (For instance, if this Fragment might inflate different views for Info, Hum, and Lyrics, then my tag names for each microphone fragment would be MetaMicButton, HumMicButton, and LyricsMicButton. I'd need to also choose between R.id.meta_mic_button_frame and so on.)

2) When you are switching between Portrait and Landscape modes, you need to define different XML layouts in the folders res/layout and res/layout_land. (Or you could make res/layout and res/layout_port if you want to consider landscape mode to be the default). Please check this link: http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources You can define different layouts based on screen size, language, dock mode, night mode, and any combination thereof.



来源:https://stackoverflow.com/questions/7800974/android-fragment-inflate-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!