【问题标题】:Fragment update with a new fragment Android带有新片段 Android 的片段更新
【发布时间】:2011-06-04 23:56:56
【问题描述】:

我有一个布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content">
        <LinearLayout android:id="@+id/linearLayout13" android:layout_height="wrap_content" android:paddingRight="70dp" android:orientation="vertical" android:paddingLeft="70dp" android:layout_width="wrap_content">
            <ImageView android:id="@+id/baby_icon" android:layout_height="wrap_content" android:src="@drawable/baby" android:clickable="true" android:layout_width="wrap_content"></ImageView>
        </LinearLayout>
    </LinearLayout>
    <fragment
        android:name="com.nicu.health.FragAFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/yellow_cardlist_fragment"
        android:layout_weight="1"
        >
    </fragment>
</LinearLayout>

在启动时,这确实显示了正确的片段(FragAFragment)。现在单击baby_icon 的按钮,我尝试删除当前片段并添加一个具有完全不同布局的新片段(FragBFragment)。

虽然我看到调用了onCreateView 方法并且它确实返回了一个非空视图,但是新片段屏幕上的UI 没有得到更新。我使用下面的代码来更新片段。

                        Fragment baby = FragBFragment.newInstance(1);
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
//                  ft.remove(getFragmentManager().findFragmentById(R.id.yellow_cardlist_fragment));
//                  ft.add(R.id.yellow_cardlist_fragment, baby);
                    ft.replace(R.id.yellow_cardlist_fragment, baby);
                    ft.addToBackStack(null);
                    Log.i(TAG, "Code for commit = "+ft.commit());

我已经尝试了删除、替换和添加的所有组合以使片段工作正常,但徒劳无功!

我还进一步尝试使用代码作为

<fragment
    android:name="com.nicu.health.FragBFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/yellow_cardlist_fragment"
    android:layout_weight="1"
    >

这确实可以在启动时显示第二个片段!!!!

我们会非常感谢您的帮助。

谢谢,

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    我的问题的答案是

    Android: can't replace one fragment with another

    我在主要活动开始时添加了动态片段,并且在单击时我做了一个 ft.replace 来替换旧片段!

    【讨论】:

      【解决方案2】:

      好吧,我的第一个猜测:你忘了说 ft.commit();

      我的第二个猜测:

      假设你有 Activity AB 来控制 Fragment A 和 Fragment B。

      您还有 3 个布局。 Layout_ActivityAB、Layout_FragA、Layout_FragB。

      所以 setContentView(R.layout.Layout_ActivityAB) 在 Activity AB 内部被调用。这将是你告诉 android 将片段放置在屏幕中的哪个位置的布局。

      在 Fragment A 或 Fragment B 内部:

      @Override
          //onCreateView is called when an instance of this class is first created.
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.Layout_FragA, container, false);  return rootView;
          }
      

      R.layout.Layout_FragA 在 Fragment 内部被调用。 Layout_FragA 是 Fragment 的实际内容。

      【讨论】:

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