微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Android新片段更新片段

我有一个布局

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

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

帮助将在reeally appreicated.

谢谢,

解决方法:

回答我的问题是

Android: can’t replace one fragment with another

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐