New Fragment不能完全替换旧片段

时间:2017-03-07 04:05:41

标签: android-fragments replace

enter image description here

这是我单击图像按钮后的布局,新片段未完全替换旧片段...

这是我的旧片段菜单java代码

public class CMenu extends Fragment implements View.OnClickListener{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState){

return inflater.inflate(R.layout.fragment_cmenu,container,false);
}

@Override
public void onViewCreated(View view,@Nullable Bundle savedInstanceState){
    super.onViewCreated(view,savedInstanceState);
    initView(view);

}
private void initView(View view){
    ImageButton prebtn = (ImageButton) view.findViewById(R.id.premium_cake);
    prebtn.setOnClickListener(this);
    ImageButton homemade = (ImageButton) view.findViewById(R.id.homemade);
    homemade.setOnClickListener(this);
    ImageButton sc_cake = (ImageButton) view.findViewById(R.id.sc_cake);
    sc_cake.setOnClickListener(this);
    ImageButton special_food = (ImageButton) 
    view.findViewById(R.id.special_food);
    special_food.setOnClickListener(this);
    ImageButton delicious_drink = (ImageButton) 
    view.findViewById(R.id.delicious_drink);
    delicious_drink.setOnClickListener(this);
    ImageButton other = (ImageButton) view.findViewById(R.id.other);
    other.setOnClickListener(this);

}

@Override
public void onClick(View view){
    switch (view.getId()){
        case R.id.premium_cake:

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
PremiumCake()).addToBackStack(null).commit();
            break;
        case R.id.homemade:

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
HomeMade()).addToBackStack(null).commit();
            break;
        case R.id.sc_cake:

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
SpecialCake()).addToBackStack(null).commit();
            break;
        case R.id.delicious_drink:

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
DeliciousDrink()).addToBackStack(null).commit();
            break;
        case R.id.special_food:

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
SignatureFood()).addToBackStack(null).commit();
            break;
        case R.id.other:

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
 Other()).addToBackStack(null).commit();
            break;
    }
}

this is my new fragment java code

public class Other extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.other, container, false);
}

旧片段xml(我正在使用滚动视图)

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="?android:windowBackground"
    android:layout_height="wrap_content"
    tools:context=".MenuFragment"
    tools:ignore="ContentDescription"
    android:id="@+id/MenuFragment">

    <ImageButton
        android:layout_width="match_parent"
        android:id="@+id/sc_cake"
        android:background="@drawable/sc_bg"
        android:layout_height="120dp"
        android:layout_below="@+id/homemade"
        android:layout_alignParentStart="true"
         />

    <ImageButton
        android:layout_width="match_parent"
        android:id="@+id/premium_cake"
        android:background="@drawable/pc_bg"
        android:layout_height="120dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="50dp" />

    <ImageButton
        android:layout_width="match_parent"
        android:id="@+id/homemade"
        android:background="@drawable/hmc_bg"
        android:layout_height="120dp"
        android:layout_below="@+id/premium_cake"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="match_parent"
        android:id="@+id/delicious_drink"
        android:background="@drawable/dd_bg"
        android:layout_height="120dp"
        android:layout_below="@+id/special_food"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="match_parent"
        android:id="@+id/other"
        android:background="@drawable/other_bg"
        android:layout_height="120dp"
        android:layout_below="@+id/delicious_drink"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="50dp" />

    <ImageButton
        android:layout_width="match_parent"
        android:id="@+id/special_food"
        android:background="@drawable/sf_bg"
        android:layout_height="120dp"
        android:layout_below="@+id/sc_cake"
        android:layout_alignParentStart="true" />

   </RelativeLayout>
</ScrollView>

新片段xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">

<GridLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:background="#2e3192">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp">

        <Button
            android:id="@+id/cb_big"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="180dp"
            android:background="@drawable/coolerbag_big" />

        <Button
            android:id="@+id/cb_small"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="180dp"
            android:background="@drawable/coolerbag_small" />


    </LinearLayout>
</GridLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

  

包含旧Fragment的Activity的XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/black"
    tools:context=".MainActivity">
<FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
  

replace方法删除了之前的片段   容器并添加一个新的。

因此,在最初加载旧片段的Activity类中,您必须完成:

OldFragment fragment = new OldFragment();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container, fragment);
        fragmentTransaction.commit();

现在,在单击图像时用新的片段替换旧片段,而不是:

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
PremiumCake()).addToBackStack(null).commit();

只是做:

 getFragmentManager().beginTransaction().replace(R.id.container,new 
    PremiumCake()).addToBackStack(null).commit();
  

container是Activity的XML中的Frame布局   你的旧片段并用新的片段替换它。

相关问题