Android getChildFragmentManager()以编程方式(动态)添加片段?
如何使用(或“我们可以使用”)Android getChildFragmentManager()以编程方式(动态)添加片段?,android,android-fragments,android-nested-fragment,Android,Android Fragments,Android Nested Fragment,如何使用(或“我们可以使用”)getChildFragmentManager()以编程方式(动态)添加Fragments 这是我的例子。 我有一个main活动,一个OuterFrag,还有一个InnerFrag。我将通过FragmentManager动态地将OuterFrag添加到main活动中。此外,我还将通过FragmentManager动态地将InnerFrag添加到OuterFrag。但是我想添加InnerFrag作为OuterFrag的子级,而不是取代OuterFrag,成为main活
getChildFragmentManager()
以编程方式(动态)添加Fragment
s
这是我的例子。
我有一个main活动
,一个OuterFrag
,还有一个InnerFrag
。我将通过FragmentManager
动态地将OuterFrag
添加到main活动中。此外,我还将通过FragmentManager
动态地将InnerFrag
添加到OuterFrag
。但是我想添加InnerFrag
作为OuterFrag
的子级,而不是取代OuterFrag
,成为main活动的新子级
我想保留这个层次结构:MainActivity->OuterFrag->InnerFrag
。因此MainActivity始终可以调用OuterFrag。
但不从该层次结构更改:MainActivity->OuterFrag
到该层次结构:MainActivity->InnerFrag
,MainActivity
将丢失OuterFrag
这是我的示例代码。
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));
}
}
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();
}
}
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);
}
}
活动\u 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>
<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" />
<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" />
outer_frag.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>
<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" />
<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" />
内部框架.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>
<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" />
<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" />
使用getChildFragmentManager()
理论上是正确的。它可以在非编程添加的片段中使用(这意味着将activity\u main.xml
的
更改为
,添加属性android:name=“com.example.frag.OuterFrag”
,并删除MainActivity.java
中的第一条getSupportFragmentManager()
语句)。它保持了正确的层次结构:MainActivity->OuterFrag->InnerFrag
。但是原始片段(outer_frag.xml
)的单词永远不会被拿走
总之,我希望始终在main活动中引用OuterFrag
。我希望OuterFrag
充当占位符来加载不同的InnerFrag
s。简而言之,当以编程方式(动态)添加时,我想在OuterFrag
中调用getChildFragmentManager()
总之,我希望始终在MainActivity中引用OuterFrag。
我希望OuterFrag充当占位符来加载不同的
内部框架。简而言之,我想在中调用getChildFragmentManager()
以编程方式(动态)添加OuterFrag时
如果需要,则将OuterFrag
的容器布局作为其内容,并向该容器添加任何InnerFrag
。OuterFrag
的布局文件为:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
用于将OuterFrag
添加到主活动的代码仍然有效。如果要在OuterFrag.java
代码中调用addinnerrag()
,可以这样做:
OuterFrag.java:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
addInnerFrag();
}
Hi@Luksprog我遵循上述方法,将内部片段添加到外部片段。但OuterFragment并没有取代内容。它被添加为视图,并且OuterFragment中以前的内容也可见。我也尝试过使用beginTransaction()。替换,但op中没有更改。请帮助我…………@KK_07k11A0585请发布一个新问题,提供有关您问题的更多详细信息。请记住,片段事务不会替换将添加片段的布局中已有的视图。如果您还想替换这些视图,请将这些初始视图放在另一个片段中。@Luksprog您好,如何将InnerDetailFrag添加到InnerFrag?@Luksprog我如何管理InnerFrag的反压以使outerFrag可见?是的,如果子片段调用MenuItems.clear(),它将被删除!!你把它弄好了吗?我的问题是何时添加InnerFrag
,使其与OuterFrag
的生命周期无缝配合?