Android的碎片膨胀布局 [英] Android Fragment Inflate layout

查看:124
本文介绍了Android的碎片膨胀布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的工作,以期寻呼机和片段。我想重写的方向变化,使onCreateView方法不是每次运行方向改变。这是因为很多运行在onCreateView我的应用程序,导致的取向是缓慢改变的。另外我的服务器上的东西进行检查并没有什么太大所以真的没有理由对所有的它运行只是因为方向变化。

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.

下面是我已经成立迄今

我有这样的MainActivity在清单

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();
}

当我运行的应用程序,并切换方向我看到敬酒演出,但布局永远不会改变的景观之一。所以我决定,也许刚刚的android没有认识到它是在景观,因为我overrided,但改变布局,以雷人的东西像一个对话框的布局不会导致强制关闭或任何它保持其拉伸纵向布局作为一个结果,所以我知道有什么问题我是如何膨胀的布局。

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.

在改变布局时的取向是在一个viewpager改变将大大AP preciated任何帮助。

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

感谢您!

推荐答案

1)你应该把所有的服务器检查程序和其它一次性行动片段的onCreate(),而不是onCreateView()。如果你需要表现出某种载入中...屏幕,同时它加载,那么我会建议使用的 FragmentActivity 的膨胀的布局,至少一个的的FrameLayout 的是包含一个载入中...类型的图形。首先,FragmentActivity将运行的onCreate(),将在其中第一膨胀的初始视图,然后实例的片段。把服务器检查code在片段的onCreate(),然后充气任何查看该片段在其onCreateView()方法。下面是我的一个项目初始化一个麦克风按钮,这实在是一个片段的示例:

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"
       />

然后,麦克风片段实际上是由以下Java code实例:

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实际上是你给一个片段实例是从片段的R.id不同的一个独特的标记名称。这样,您就可以使用findFragmentByTag(),以确保碎片得到所有你需要他们的地方正确实例。

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.

您可以按摩此codeA点点,如果你正在寻找装载片段中的片段,就像我的应用程序实际上是如何工作的。如果你有一个片段,将膨胀的几种可能的一个视图,然后使用if语句来选择合适的帧ID,并指定一个唯一的FRAG_TAG。我会建议使用不同的R.id的为您创造一个片段各的FrameLayout。 (例如,如果该片段可能会膨胀的信息,哼哼,和歌词不同的意见,然后我对每个麦克风片段标记名称将是 MetaMicButton HumMicButton LyricsMicButton 的。我还需要R.id.meta_mic_button_frame之间等进行选择。)

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)当你在纵向和横向模式之间切换,你需要在文件夹RES /布局和RES / layout_land定义不同的XML布局。 (或者你可以让RES /布局和RES / layout_port,如果你要考虑横向模式为默认)。请检查此链接:<一href="http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources" rel="nofollow">http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources可以根据屏幕大小,语言,坞模式,夜景模式定义不同的布局,以及它们的任意组合

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.

这篇关于Android的碎片膨胀布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆