简体   繁体   English

片段设计

[英]Fragment Design

I'm having a problem with fragments in my application. 我的应用程序中存在片段问题。 My interface looks like this: 我的界面如下所示:

Screen 1
+---------------+
|   Fragment 1  |
+---------------+
|   Fragment 2  |
|               |
|               |
+---------------+

Screen 2
+---------------+
|   Fragment 1  |
+---------------+
|   Fragment 3  |
|               |
|               |
+---------------+

Each screen is a separate activity and these fragments are defined in my layout .xml files (therefore replacing dynamically probably won't work). 每个屏幕都是一个单独的活动,这些片段在我的布局.xml文件中定义(因此,动态替换可能不起作用)。

My question is: 我的问题是:

Both screen1 and screen2 contain a fragment1, and all the information that goes with it. screen1和screen2都包含一个fragment1,以及随之而来的所有信息。 As it stands, I am currently passing all the data for fragment1 when I start the screen2 activity - I then populate fragment1 in screen2 with the data from fragment1 screen1. 就目前而言,当我启动screen2活动时,我当前正在传递fragment1的所有数据-然后,我用来自fragment1 screen1的数据填充screen2中的fragment1。 If the user hits the back button, I have to populate fragment1 screen1 with whatever was changed in screen2. 如果用户单击“后退”按钮,则必须用screen2中更改的内容填充fragment1 screen1。 It's a big headache. 真是头疼

Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

(I'd prefer not to use the single-activity/multiple-fragment approach since I've already been there and decided that multiple-activity/multiple-fragment works better for this application.) (我不希望使用单一活动/多片段方法,因为我已经去过那里,并决定多重活动/多片段对此应用程序更好。)

Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

Yes, use a single Activity , and have Fragment 3 replace Fragment 2 when you need to add it, so Fragment 1 will remain the same with all of its data. 是的,使用单个Activity ,并在需要添加Fragment 3时将其replace Fragment 2 ,因此Fragment 1所有数据将保持不变。

(I'd prefer not to use the single-activity/multiple-fragment approach since I've already been there and decided that multiple-activity/multiple-fragment works better for this application.) (我不希望使用单一活动/多片段方法,因为我已经去过那里,并决定多重活动/多片段对此应用程序更好。)

I'd have to disagree, since you have chosen the harder route by setting your Fragments in xml and passing around their data between Activity s. 我不得不不同意,因为您通过在xml中设置Fragments并在Activity之间传递它们的数据来选择了更困难的路线。 I can almost guarantee that is more work than setting FrameLayout containers in your layout and then simply adding/replacing Fragment s in them. 我几乎可以保证,比在布局中设置FrameLayout容器然后在其中简单地添加/替换Fragment FrameLayout工作还要多。

Simple example - http://developer.android.com/training/basics/fragments/fragment-ui.html 简单示例-http://developer.android.com/training/basics/fragments/fragment-ui.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM