[英]Fragment update with a new fragment Android
I have a layout of我有一个布局
<?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>
On the startup this does show the correct fragment(FragAFragment).在启动时,这确实显示了正确的片段(FragAFragment)。 Now on the button click for baby_icon
I try to remove the current fragment and add a new one(FragBFragment) which has an entirely different layout.现在在按钮上单击baby_icon
我尝试删除当前片段并添加一个具有完全不同布局的新片段(FragBFragment)。
Though I see that the onCreateView
method is called and it does return a non-null view but the UI on the screen of the new fragment does not get updated.虽然我看到调用了onCreateView
方法并且它确实返回了一个非空视图,但是新片段屏幕上的 UI 没有得到更新。 I use the below code to update the fragment.我使用下面的代码来更新片段。
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());
I had tried all combinations of remove, replace and add to get the fragment thing workng, but in vain!我尝试了删除、替换和添加的所有组合以使片段工作正常,但徒劳无功!
Also further I tried with the code as我还进一步尝试使用代码
<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"
>
and this does work to show the second fragment on startup!!!!这确实可以在启动时显示第二个片段!!!!
Help will be reeally appreicated.帮助将得到真正的赞赏。
Thanks,谢谢,
Answer to my question is我的问题的答案是
Android: can't replace one fragment with another Android:不能用另一个片段替换一个片段
I added dynamic fragments on start of the main activity and onclick I did a ft.replace to replace the old fragment!我在主要活动开始时添加了动态片段,onclick 我做了一个 ft.replace 来替换旧片段!
Ok, my first guess: You forgot to say ft.commit();好吧,我的第一个猜测:你忘了说 ft.commit();
My second guess:我的第二个猜测:
Lets say u have Activity AB which controls Fragment A and Fragment B.假设你有控制片段 A 和片段 B 的 Activity AB。
You also have 3 layouts.您还有 3 个布局。 Layout_ActivityAB, Layout_FragA, Layout_FragB. Layout_ActivityAB、Layout_FragA、Layout_FragB。
So setContentView(R.layout.Layout_ActivityAB) is called inside Activity AB.所以 setContentView(R.layout.Layout_ActivityAB) 在 Activity AB 内部被调用。 That will be the layout in which u tell android where to place the fragment in the screen.这将是您告诉 android 将片段放置在屏幕中的哪个位置的布局。
Inside Fragment A or Fragment B:在 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 is called inside the Fragment. R.layout.Layout_FragA 在 Fragment 内部被调用。 Layout_FragA is the actual content of the Fragment. Layout_FragA 是 Fragment 的实际内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.