问题描述
如何使用(或我们可以用) getChildFragmentManager()
上编程(动态)加入片段
S'
这是我的榜样。
我有一个 MainActivity
,一个 OuterFrag
,一个 InnerFrag
。我将添加 OuterFrag
到 MainActivity
动态的 FragmentManager
。而且,我将添加 InnerFrag
到 OuterFrag
还可以动态地由 FragmentManager
。不过,我想增加 InnerFrag
完全一样的 OuterFrag
,而不是取代 OuterFrag ,并成为新的子
MainActivity
。
我想保持这种层次结构: MainActivity - > OuterFrag - > InnerFrag
。所以MainActivity可以随时调用OuterFrag。
但是从这个层次结构不发生变化: MainActivity - > OuterFrag
此层次结构: MainActivity - > InnerFrag
的 MainActivity
将损失 OuterFrag
。
这是我的例子code。
MainActivity.java
包com.example.frag;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
公共类MainActivity扩展FragmentActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
。getSupportFragmentManager()的BeginTransaction()加(R.id.frameLayout,新OuterFrag())提交()。
getSupportFragmentManager()executePendingTransactions()。
的System.out.println(前:
+ getSupportFragmentManager()findFragmentById(R.id.frameLayout))。
((OuterFrag)getSupportFragmentManager()。findFragmentById(R.id.frameLayout))
.addInnerFrag();
的System.out.println(后
+ getSupportFragmentManager()findFragmentById(R.id.frameLayout))。
}
}
activity_main.xml
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /的FrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< /的FrameLayout>
OuterFrag.java
包com.example.frag;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
公共类OuterFrag扩展片段{
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
返回inflater.inflate(R.layout.outer_frag,集装箱,假);
}
公共无效addInnerFrag(){
。getFragmentManager()的BeginTransaction()代替(this.getId(),新InnerFrag())提交()。
getFragmentManager()executePendingTransactions()。
// getChildFragmentManager()的BeginTransaction()加(this.getId(),新InnerFrag())提交()。
// getChildFragmentManager()executePendingTransactions()。
}
}
outer_frag.xml
< TextView中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=我是外断枝/>
InnerFrag.java
包com.example.frag;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
公共类InnerFrag扩展片段{
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
返回inflater.inflate(R.layout.inner_frag,集装箱,假);
}
}
inner_frag.xml
< TextView中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / textView2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=我是内断枝/>
目前,上述code能不出错运行。但它实际上是改变 InnerFrag
为 MainActivity
的新子。这可以由两个系统进行验证打印出的语句,该前:对象和后:对象被改变。在 OuterFrag.java
,如果 getChildFragmentManager()
语句运行,而不是 getFragmentManager ()
语句,我们将获得以下运行时错误:
2月12号至七号:29:38.406:E / AndroidRuntime(12051):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.frag / com.example。 frag.MainActivity}:java.lang.IllegalArgumentException:如果没有查看发现ID 0x7f070000(com.example.frag:ID /的FrameLayout)的片段InnerFrag {46e32748#0 ID = 0x7f070000}
使用 getChildFragmentManager()
在理论上是正确的。它可以在非编程方式添加片段中使用(这意味着改变 activity_main.xml
的<的FrameLayout>
以<片断>
,增加属性安卓名=com.example.frag.OuterFrag
,并删除第一个在
语句)。它是保持正确的层次结构: MainActivity.java
getSupportFragmentManager() MainActivity - > OuterFrag - > InnerFrag
。但原来的片段的话( outer_frag.xml
)永远带走。
在最后,我想引用 OuterFrag
在 MainActivity
始终。我希望 OuterFrag
充当占位符加载不同的 InnerFrag
秒。总之,我要打电话 getChildFragmentManager()
在 OuterFrag
,当编程方式添加(动态的)。
如果你想要这个,然后让你的 OuterFrag
有一个容器布局作为其内容和添加任何 InnerFrag
到容器。对于布局文件中的 OuterFrag
将是:
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / fragContainer
机器人:layout_width =match_parent
机器人:layout_height =match_parent
/>
当然,如果你想,你可以在对 OuterFrag
布局等意见。那么你的 addInnerFrag
方法是:
公共无效addInnerFrag(){
。getChildFragmentManager()的BeginTransaction()加(R.id.fragContainer,新InnerFrag())提交()。
getChildFragmentManager()executePendingTransactions()。
}
在$ C $下添加 OuterFrag
的主要活动仍然有效。
How to use (or "Can we use") getChildFragmentManager()
on programmatically (dynamically) added Fragment
s?
Here is my example.
I have one MainActivity
, one OuterFrag
, and one InnerFrag
. I will add the OuterFrag
to MainActivity
dynamically by the FragmentManager
. And also, I will add the InnerFrag
to the OuterFrag
also dynamically by the FragmentManager
. But I want to add InnerFrag
exactly as a child of the OuterFrag
, not replacing OuterFrag
and be the new child of the MainActivity
.
I want to keep this hierarchy: MainActivity -> OuterFrag -> InnerFrag
. So MainActivity can always call OuterFrag.
But NOT change from this hierarchy: MainActivity -> OuterFrag
to this hierarchy: MainActivity -> InnerFrag
that MainActivity
will loss the OuterFrag
.
Here is my example code.
MainActivity.java
package com.example.frag;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, new OuterFrag()).commit();
getSupportFragmentManager().executePendingTransactions();
System.out.println("Before: "
+ getSupportFragmentManager().findFragmentById(R.id.frameLayout));
((OuterFrag) getSupportFragmentManager().findFragmentById(R.id.frameLayout))
.addInnerFrag();
System.out.println("After: "
+ getSupportFragmentManager().findFragmentById(R.id.frameLayout));
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
OuterFrag.java
package com.example.frag;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OuterFrag extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.outer_frag, container, false);
}
public void addInnerFrag() {
getFragmentManager().beginTransaction().replace(this.getId(), new InnerFrag()).commit();
getFragmentManager().executePendingTransactions();
// getChildFragmentManager().beginTransaction().add(this.getId(), new InnerFrag()).commit();
// getChildFragmentManager().executePendingTransactions();
}
}
outer_frag.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="i am the OUTER frag" />
InnerFrag.java
package com.example.frag;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class InnerFrag extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.inner_frag, container, false);
}
}
inner_frag.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="i am the INNER frag" />
Currently, the above code can run without errors. But it is actually changing InnerFrag
as the new child of MainActivity
. This can be verified by the two System Print Out statements that the Before: Object and the After: Object is changed. In the OuterFrag.java
, if the getChildFragmentManager()
statements are run instead of the getFragmentManager()
statements, we will get the following runtime error:
12-07 02:29:38.406: E/AndroidRuntime(12051): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.frag/com.example.frag.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.example.frag:id/frameLayout) for fragment InnerFrag{46e32748 #0 id=0x7f070000}
Using getChildFragmentManager()
is theoretically correct. It can be used in a non-programmatically added fragment (which means changing activity_main.xml
's <FrameLayout>
to <fragment>
, add attribute android:name="com.example.frag.OuterFrag"
, and remove the first getSupportFragmentManager()
statement in MainActivity.java
). And it is keeping the correct hierarchy: MainActivity -> OuterFrag -> InnerFrag
. But the words of original fragment (outer_frag.xml
) can never be taken away.
In conclusion, I want to reference OuterFrag
in MainActivity
always. And I want OuterFrag
act as a placeholder to load different InnerFrag
s. In short, I want to call getChildFragmentManager()
in OuterFrag
, when it is added programmatically (dynamically).
If you want this then make your OuterFrag
have a container layout as its content and add whatever InnerFrag
to that container. The layout file for the OuterFrag
will be:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Of course you could have other views in the layout for the OuterFrag
if you want that. Then your addInnerFrag
method will be:
public void addInnerFrag() {
getChildFragmentManager().beginTransaction().add(R.id.fragContainer, new InnerFrag()).commit();
getChildFragmentManager().executePendingTransactions();
}
The code for adding the OuterFrag
to the main activity remains valid.
这篇关于getChildFragmentManager()的编程方式(动态)增加碎片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!