简体   繁体   English

Android:来自同一XML文件的多个片段

[英]Android: Multiple Fragments from same XML file

I have a view pager that has a page for each subject in an array (English, Maths, Science, etc). 我有一个视图寻呼机,它有一个数组中每个主题的页面(英语,数学,科学等)。 The fragments in the view pager are all created from the same XML file. 视图分页器中的片段都是从同一XML文件创建的。 These fragments then have fragments added inside of them. 然后这些片段在其内部添加了片段。 As all the parents inflate from the same XML file, I can not specify which fragment I want to add the child fragment to. 由于所有父级都从同一个XML文件中膨胀,我无法指定要将子片段添加到哪个片段。 I was wondering if their was a way to do this - Narrow the scope of the id maybe?? 我想知道他们是否有办法做到这一点 - 缩小id的范围也许?

First. 第一。 I suggest you to try to avoid child fragment, if you really doesn't have very good reason to use them. 如果你真的没有很好的理由使用它们,我建议你尽量避免儿童碎片。

But I don't understand where is problem. 但我不明白问题出在哪里。 I don't see any problem if more fragment use the same XML for the inflate. 如果更多的片段使用相同的XML进行膨胀,我没有看到任何问题。 You can call setArguments() on all parent fragment and put here some Bundle which can be used to recognize which fragment it is inside of it by getArguments() . 你可以在所有父片段上调用setArguments()并在这里放置一些Bundle,它可以用来通过getArguments()来识别它里面的片段。

I think you want to implement multiple fragments with the capability to manipulate them depending on user selections. 我认为您希望实现多个片段,并具有根据用户选择操作它们的功能。 There is Google link Fragments 有Google链接片段

Review these code on that web page: 在该网页上查看以下代码:

  1. The many samples of Fragment and FragmentTransaction FragmentFragmentTransaction的许多样本
  2. Look at method showDetails in class TitlesFragment , for example. 例如,查看类TitlesFragment中的方法showDetails

If you want a working sample, look at Working with Android Fragments . 如果您需要工作样本,请查看使用Android片段 Snippet from the webpage: 来自网页的片段:

<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="match_parent">
  <fragment
    android:id="@+id/headFrag"
    android:name="com.intertech.HeaderFrag"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>
  <fragment
    android:id="@+id/bodyFrag"
    android:name="com.intertech.BodyFrag"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>
</LinearLayout>

And the code 和代码

...
    FragmentManager fragmentManager = getFragmentManager ();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
    // work here to change Activity fragments (add, remove, etc.).
    fragmentTransaction.add (R.id.myFrame, myFrag);
    fragmentTransaction.commit ();

From above, notice there are 2 fragments in the layout. 从上面看,布局中有2个片段。 And fragmentTransaction.add method should specify parent container in R.id form. 而fragmentTransaction.add方法应该以R.id形式指定父容器。

Someday I may want/need to implement this. 有一天,我可能想要/需要实现这一点。 So far I implemented show/hide Layouts instead (not regretting it), and they can have IDs for me to manipulate. 到目前为止,我实现了显示/隐藏Layouts (不后悔),并且他们可以使用ID来操作。 Have fun and keep us posted, Tommy Kwee. Tommy Kwee,玩得开心,让我们发布信息。

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

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