gpt4 book ai didi

android - 适用于 API 17 之前版本的嵌套 fragment

转载 作者:行者123 更新时间:2023-11-29 15:55:54 27 4
gpt4 key购买 nike

我很困惑。 Here it says:

nested fragments as shown in the screenshot are not possible without the support library.

还有here they said :

AFAIK, fragments cannot hold other fragments.

这一切都得到了 Android Documentation here 的证实。 .所以这意味着一件事:

You can't put Fragments into Fragments without Support v4 Library on pre API 17!

好的,但是当我有以下设置时:构建.gradle

apply plugin: 'com.android.application'


android {
compileSdkVersion 16
buildToolsVersion '19.1.0'

defaultConfig {
applicationId "package.supportfragments"
minSdkVersion 14
targetSdkVersion 16
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

主要 Activity :

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getFragmentManager().beginTransaction().add(R.id.frame_test, new TestFragment()).commit();
}

测试 fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container, false);

getFragmentManager().beginTransaction().add(R.id.frame_test_2, new TestNestedFragment()).commit();

return rootView;
}

测试嵌套 fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test_nested, container, false);

getFragmentManager().beginTransaction().add(R.id.frame_test_3, new TestNestedNestedFragment()).commit();

return rootView;
}

布局看起来都是这样的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- TODO: Update blank fragment layout -->
<FrameLayout
android:id="@+id/IDTOTHEFRAGMENT"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

它就像一个魅力...问题是..为什么??!!

最佳答案

您的示例没有嵌套 fragment 。从 fragment 调用 getFragmentManager() 时,它会返回 Activity FragmentManager,因此您要将 fragment 添加到 Activity 中。

要添加嵌套 fragment ,您需要使用API 17 中引入的getChildFragmentManager(),因此如果您不使用支持 fragment 实现,则无法使用它。

关于android - 适用于 API 17 之前版本的嵌套 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27901013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com