我应该如何构建在一个单一的活动架构实现多片段配置(例如主 - 详细信息)时,布局文件?详细信息、架构、片段、布局

2023-09-04 12:28:08 作者:煙ゝ刺痛了眼伤了肺

我目前切换到单一的活动架构将管理加载片段的容器布局和退出 。事情进展顺利,但我遇到了阻碍。我要实现的主 - 的模式,但我想不出来处理布局文件的最好方法。有迹象表明,我已经想到了两种方法,但两者产生的后果,我要么不喜欢或不确定。

I am currently switching to a single activity architecture that will manage loading fragments in and out of a container layout. Things are going well, but I have hit a snag. I want to implement the master-detail pattern, but I can't figure out the best way to handle the layout file. There are two methods that I have thought of, but both have consequences that I either don't like or are unsure of.

在单独的布局陆文件与第二个容器:这里的问题是,我不希望第二个容器占用空间的时候我不在多-Fragment的配置,所以我预见了很多的纲领性布局参数的变化,可能会导致混乱。 嵌套的片段:我可以加载在一个特殊的多配置片段,其布局XML包含嵌套在适当的片段在 seperate layout-land file with a second container: The problem here is that I don't want the second container to take up space when I am not in a multi-fragment configuration, so I forsee a lot of programmatic layout param changes that could get messy. nested fragments: I could load in a special multi-configuration fragment whose layout XML contains the appropriate fragments nested within

我主要是找一些指点一下哪种方​​式我应该采取,无论是我刚才提到的两种选择,或者我可以俯瞰另一种方法。

I am mainly looking for some guidance about which approach I should take, be it the two options I have mentioned or another method that I am overlooking.

编辑:我把一个谷歌绘制,说明了两种观点我有迄今。

I put together a Google drawing that illustrates the two ideas I've had thus far.

推荐答案

编辑: 从意见的讨论中,我看你的主要问题似乎是方向变化的处理。

From the discussion in the comments, I see that your major problem seems to be the handling of the orientation change.

灵光说构造发生变化的时候(与你 基本回到的onCreate(),您需要检查 getResources()。getConfiguration()。方向。根据是, 你必须检查是否需要删除或添加片段/秒。因此,如 你有肖像智能手机,只有一个片段是present。 然后你改变为横向,你要么

As Emmanuel said, when a configuration change occurred (and you're basically back to onCreate(), you need to check for getResources().getConfiguration().orientation. According to that, you have to check if you need to remove or add fragment/s. So e.g. you have a smartphone in portrait, only one Fragment is present. Then you change to landscape, you have to either 添加一个片段到老布局或 删除全片段栈,并添加新的布局

有没有办法用布局参数改变处理(如设置从去可见,能见度等),如果你想使用旧的布局为您的片段容器。如果你不使用你的旧的布局,并创建一个新的,你必须保存用户界面状态,从而使用户得到他的选择,等回来(你知道这东西可能是 - 使用意图,等等)。

There's no way out of dealing with layout param changes (like setting visibility from GONE to VISIBLE, etc.), if you want to use an old layout for your Fragment container. If you don't use your old layout and create a new one, you have to save the UI state, so that the user gets his selections, etc. back (you know this stuff probably - use Intents, etc.).

我preFER来创建一个新的配置一个新的布局和重建国家,所以我做这样的事在的onCreate()

I prefer to create a new layout for a new configuration and recreate the state, so I do something like this in onCreate():

if (CheckApp.isScreenBig()) {
        setContentView(R.layout.frag_multi);
    } else {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // search and remove former fragments, if there were any
            setContentView(R.layout.frag_multi);
        } else {
            if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
                getSupportFragmentManager().beginTransaction().add(android.R.id.content, new FragSingle()).commit(); // single Fragment
            }
        }
    }

检测多的配置,例如像这样的Determine如果该设备是智能手机或平板电脑?

在你的活动,开始在的onCreate

if (App.isScreenBig()) {
        setContentView(R.layout.frag_multi);
    } else {
        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, new FragMainPaging()).commit(); // single Fragment
        }
    }

根据您的整体设计,你不需要做任何事情更多。如果您需要片段之间进行互动,你可以传播通过你的父母片段的/活动的ViewPager或更改控制器或任何你在的地方。

Depending on your overall design, you don't need to do anything more. If you need to interact between your Fragments, you can propagate changes through your Parent Fragment's/Activity's ViewPager or a Controller or whatever you have in place.

我通常使用嵌套的片段,并使用类似的东西进行沟通修改

I usually go with nested fragments, and use something like that to communicate changes

((FragManagedPaging) getParentFragment()).forwardChanges(NotifyState.NOTIFY_ALL, result);

NotifyState 是一个枚举,指示更改是否适用于所有片段或某几个(我通过在类名,如果我想一定的),或者如果我想创建特定事件的发生( NotifyState.ADD )等父片段承载其他片段(也是ViewPager,如果需要的话),并转发的变化,如果一个新的片段存在其他碎片present。另一个片段有一个通知方法,它指示做的工作。

NotifyState is an enum indicating if the change is relevant for all Fragments or certain ones (I pass in the Class names if I want certain ones) or if I want to create a new Fragment for a certain event (NotifyState.ADD), etc. The parent fragment hosts the other Fragments (also a ViewPager, if needed) and forwards changes, if there are other Fragments present. The other Fragments have a notify method, that indicates to do work.

void onChangeOccurred(Object... args) {
// look what changed, what work to do in args, etc.

但总体来说它是由你来决定。

But overall it's up to you to decide.

多面板布局为例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@id/frag_travel_list"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        class="de.einschnaehkeee.travel.check.frag.FragTravelBags" />

    <fragment
        android:id="@id/frag_travel_items"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        class="de.einschnaehkeee.travel.check.frag.FragBag" />

    <fragment
        android:id="@id/frag_choose_items"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        class="de.einschnaehkeee.travel.check.frag.ChooseItemsFragment" />

</LinearLayout>

有关多窗格布局配置,采用适当的布局目录中,如布局陆/ frag_multi.xml 等。

For configurations with multi pane layouts, use appropriate layout directories, like layout-land/frag_multi.xml, etc.