【问题标题】:How to add dynamic fragment in nested fragment with three pan layout如何在具有三个平移布局的嵌套片段中添加动态片段
【发布时间】:2014-04-27 13:52:15
【问题描述】:

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

<?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,并且在嵌套片段中,片段必须动态添加到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);
        }
    }

}

三个片段是这样的:

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;
   }
}

【问题讨论】:

  • 向我们展示您用于动态分配片段的代码。
  • @Pork 我已添加。看看更新。

标签: android android-fragments android-nested-fragment


【解决方案1】:

将以下内容移至 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();

【讨论】:

  • 它不工作!它像以前一样显示白屏。如果我让 layout_width = "200dp" 而不是 0dp,它就会显示出来。但我不需要那个:(
  • 即使 layout_width 为 200dp,是否不会产生 3 个大小相同的列?
  • 不,我只为第一个片段设置了 layout_width=200dp 并且只显示了这个
【解决方案2】:

无需在onActivityCreated 中添加fragments。只需将android:orientation="vertical" 更改为horizontal。除了它,一切都很好!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多