gpt4 book ai didi

android - 如何在具有三个平移布局的嵌套 fragment 中添加动态 fragment

转载 作者:行者123 更新时间:2023-11-29 16:06:10 26 4
gpt4 key购买 nike

我正在尝试构建一个三 Pane 布局(三个 ListFragment)。我可以通过以下布局在 Activity 中创建它

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/category_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<Fragment android:name="com.example.android.fragments.HeadlinesFragment2"
android:id="@+id/sub_category_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<Fragment android:name="com.example.android.fragments.HeadlinesFragment3"
android:id="@+id/sub_sub_category_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

</LinearLayout>

但我使用的是 nested fragment 并且在嵌套 fragment 中, fragment 必须动态添加到 FrameLayout 中,这就是我编写这些代码的原因:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/category_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<FrameLayout
android:id="@+id/sub_category_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<FrameLayout
android:id="@+id/sub_sub_category_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

</LinearLayout>

但它不起作用。我该怎么做?

编辑:

public class CompetitiveProgramming extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.competitive_programming_exercise, container, false);
Fragment a = new HeadlinesFragment();
Fragment b = new HeadlinesFragment2();
Fragment c = new HeadlinesFragment3();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.category_fragment, a );
transaction.add(R.id.sub_category_fragment, b );
transaction.add(R.id.sub_sub_category_fragment, c );
transaction.commit();
return rootView;
}

@Override
public void onDetach() {
super.onDetach();

try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);

} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

}

三个 fragment 是这样的:

public class HeadlinesFragment extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView=new TextView(getActivity());
textView.setText("Hello I am fragment C");
return textView;
}
}

最佳答案

将以下内容移至onActivityCreated

    Fragment a = new HeadlinesFragment();
Fragment b = new HeadlinesFragment2();
Fragment c = new HeadlinesFragment3();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.category_fragment, a );
transaction.add(R.id.sub_category_fragment, b );
transaction.add(R.id.sub_sub_category_fragment, c );
transaction.commit();

关于android - 如何在具有三个平移布局的嵌套 fragment 中添加动态 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18176868/

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