为什么我无法在Android中的两个片段之间切换

时间:2018-08-14 19:20:00

标签: android android-fragments

我已经尝试了所有方法,但仍然无法将Fragments从CalculatorP1更改为Calculator_P2。当我单击按钮时,仅显示一个片段,但是当我再次单击按钮时,第二个片段未在APP中显示。我尝试了所有操作,还尝试使用两个按钮bt仅显示一个片段。第二个片段未显示。如果我切换片段,则仅第一个片段显示bt,第二个片段不显示自身。 请帮忙。

My App... See how the button text changes but the Fragment remains the same

我的XML代码如下:

<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<LinearLayout
    android:layout_width="98dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:orientation="vertical">

    <Button
        android:id="@+id/bn_f1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment 1" />

    <Button
        android:id="@+id/bn_f2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment 2" />
</LinearLayout>

<LinearLayout
    android:id="@+id/fragment_container"
    android:layout_width="276dp"
    android:layout_height="350sp"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"></LinearLayout>

我的Activity.java如下:

package com.example.sudeepsarker.sudeepcalculatorvv;

    import android.content.Intent;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

public class MainActivity extends AppCompatActivity {


private Button bn_fragment;
private boolean status= false;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bn_fragment = findViewById(R.id.bn_f1);

    bn_fragment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            android.app.FragmentManager fragmentManager = getFragmentManager();
            android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            if(!status){

               CalculatorP1 p1 = new CalculatorP1();
               fragmentTransaction.add(R.id.fragment_container,p1);
               fragmentTransaction.commit();
               bn_fragment.setText("Fragment 2");
               status=true;
            }
            else{

                Calculator_P2 p2= new Calculator_P2();
                fragmentTransaction.add(R.id.fragment_container,p2);
                fragmentTransaction.commit();
                bn_fragment.setText("Fragment 1");
                status=false;
            }

        }
    });
}

}

我的片段一个xml文件:

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="350sp"
tools:context=".CalculatorP1"
android:background="#8F3A1E"
>

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="My first Part of calculator"
    android:textColor="#ffffff"
    android:layout_gravity="center"
    android:gravity="center"
    android:textAppearance="?android:textAppearanceLarge"
    />

我的片段二XML文件:

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="350sp"
tools:context=".CalculatorP1"
android:background="#077715"
>

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="My Second Part of calculator"
    android:textColor="#ffffff"
    android:layout_gravity="center"
    android:gravity="center"
    android:textAppearance="?android:textAppearanceLarge"
    />

0 个答案:

没有答案