在按钮单击时添加和替换一个片段与另一个片段

时间:2017-05-02 11:17:29

标签: android android-fragments

我有两个片段,WishlistFragment和GoShoppingFragment以及一个按钮"继续购物"。点击按钮,我想用GoShoppingFragment取代WishlistFragment。

这是onClick的实现。

public void onClickShopNow() {
    FragmentManager fragmentManager = getFragmentManager();
    Fragment fragment = new GoShoppingFragment();
    fragmentManager
            .beginTransaction()
            .replace(R.id.container, fragment)
            .addToBackStack(null)
            .commit();
    if(fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStackImmediate();
    }
}

这里的问题是,当我点击按钮"继续购物"然后WishlistFragment得到了GoShoppingFragment的关联,但是得到了像this这样的输出。 WishlistFragment仍然在后台。我该如何解决这个问题?

GoShopping布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.store.goshopping.GoShoppingFragment">
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_go_shopping"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_go_shopping"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="visible" />
<LinearLayout
    android:id="@+id/moreLoading"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="@color/transparent"
    android:gravity="center"
    android:orientation="horizontal"
    android:visibility="gone">
    <ProgressBar
        android:id="@+id/moreLoadingIndicator"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:text="@string/label_loading_more" />
</LinearLayout></RelativeLayout>

愿望清单布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.launchbyte.appio.ui.store.mywishlist.MyWishlistFragment">

<LinearLayout
    android:id="@+id/layout_continue_shopping"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:visibility="gone">

    <TextView
        android:id="@+id/txt_wishlist_empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="8dp"
        android:text="@string/wishlist_empty" />

    <TextView
        android:id="@+id/txt_wishlist_add_items"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="8dp"
        android:text="@string/wishlist_add_items" />

    <Button
        android:id="@+id/button_continue_shopping"
        android:layout_width="150dp"
        android:layout_height="48dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:background="@drawable/primary_color_button_selector"
        android:text="@string/continue_shopping" />
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_my_wishlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fadeScrollbars="true"
        android:visibility="gone" />

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="visible" />
</RelativeLayout></FrameLayout>

4 个答案:

答案 0 :(得分:1)

在每个片段xml中设置片段的背景和setClickable(true)。

答案 1 :(得分:1)

在父级布局中将背景色设置为“白色”即可解决此问题。

答案 2 :(得分:0)

删除它:

if(fragmentManager.getBackStackEntryCount() > 0) {
    fragmentManager.popBackStackImmediate();
}

同样在你的GoShopping layout:根标签中添加:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="?android:attr/windowBackground"
tools:context=".ui.store.goshopping.GoShoppingFragment">

答案 3 :(得分:0)

public void onClickShopNow() {

    FragmentManager fragmentManager = getFragmentManager();

    Fragment fragment = new GoShoppingFragment();
    Fragment WishlistFragment = 
    fragmentManager.getFragmentByTag("TagName");

    fragmentManager
            .beginTransaction()
            .remove(WishlistFragment)
            .commit();

    fragmentManager
            .beginTransaction()
            .add(R.id.container, fragment,"TagHere")
            .commit();

}

我希望这能帮到你..