gpt4 book ai didi

android - 使用新 fragment Android fragment 更新

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:22 25 4
gpt4 key购买 nike

我有一个布局

    <?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>

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

尽管我看到调用了 onCreateView 方法并且它确实返回了一个非空 View ,但是新 fragment 屏幕上的 UI 没有得到更新。我使用以下代码更新 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());

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

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

<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"
>

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

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

谢谢,

最佳答案

我的问题的答案是

Android: can't replace one fragment with another

我在主要 Activity 和 onclick 开始时添加了动态 fragment ,我做了一个 ft.replace 来替换旧 fragment !

关于android - 使用新 fragment Android fragment 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240117/

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