如何在Android中将片段添加到Activity中

时间:2016-08-27 09:59:57

标签: android android-fragments

我希望将fragment添加到activity,然后点击此按钮切换 fragments之间的按钮!
我希望在Fragment_1默认显示 activity 我的意思是:,在运行activity时显示fragment_1 )点击按钮切换fragment_2fragment_1

之间

我在下面编写代码,在运行时显示activity显示fragment_1但是当点击按钮在fragments之间切换时,请显示强制关闭错误。

FC错误:

    08-27 14:15:41.363 14224-14224/com.mohammad.nouri.diaryday E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.mohammad.nouri.diaryday, PID: 14224
 java.lang.IllegalStateException: commit already called
     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:641)
     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:621)
     at com.mohammad.nouri.diaryday.Activities.LoginActivity$1.onClick(LoginActivity.java:58)
     at android.view.View.performClick(View.java:4764)
     at android.view.View$PerformClick.run(View.java:19844)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5349)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

活动代码:

public class LoginActivity extends AppCompatActivity {

    private Context context;
    private boolean status = false;
    FragmentTransaction fT;
    FragmentManager fM;

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

        context = this;

        Glide.with(this).load(R.drawable.login_header_image)
                .bitmapTransform(new BlurTransformation(context, 20))
                .into((ImageView) findViewById(R.id.login_background));

        fM = getSupportFragmentManager();
        fT = fM.beginTransaction();

        if (!status) {
            LoginFragment f1 = new LoginFragment();
            fT.add(R.id.login_cardView, f1);
            fT.commit();
            status = true;
        }

        FloatingActionButton f = (FloatingActionButton) findViewById(R.id.login_registerButton);
        f.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (!status) {
                    RegisterFragment f1 = new RegisterFragment();
                    fT.add(R.id.login_cardView, f1);
                    fT.commit();
                    status = true;
                }else {
                    LoginFragment f2 = new LoginFragment();
                    fT.add(R.id.login_cardView, f2);
                    fT.commit();
                    status = false;
                }

            }
        });
    }
}

我该如何解决?谢谢所有< 3

3 个答案:

答案 0 :(得分:0)

而不是添加fragments尝试替换,因为您已经在上面添加了一次。

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button btnFragment;
    LinearLayout fragmentContainer;
    boolean toggleFlag = false;

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

        fragmentContainer = (LinearLayout) findViewById(R.id.fragment_container);

        FragmentA fragmentA = new FragmentA();
        final FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragmentA);
        fragmentTransaction.commit();

        btnFragment = (Button) findViewById(R.id.btn_fragment);
        btnFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(toggleFlag) {
                    FragmentA fragmentA = new FragmentA();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.fragment_container, fragmentA);
                    fragmentTransaction.commit();
                } else {

                    FragmentB fragmentB = new FragmentB();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.fragment_container, fragmentB);
                    fragmentTransaction.commit();
                }
                toggleFlag = !toggleFlag;
            }
        });
    }
}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>

<强> FragmentA.java

public class FragmentA extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.layout_fragment_a, null);
        return rootview;
    }

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

<强> layout_fragment_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/picture"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Demo Fragment A"
        android:textColor="@android:color/black"/>

</LinearLayout>

<强> FragmentB.java

public class FragmentB extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.layout_fragment_b, null);
        return rootview;
    }

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

<强> layout_fragment_b.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/picture"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Demo Fragment B"
        android:textColor="@android:color/black"/>

</LinearLayout>

答案 1 :(得分:0)

您只能在commit() FragmentTransaction中拨打onCreate()。因此,只需在您的工厂内创建一个新的FragmentTransaction

FloatingActionButton f = (FloatingActionButton) findViewById(R.id.login_registerButton);
        f.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View view) {


                if (!status) {
                    RegisterFragment f1 = new RegisterFragment();
                    fM.beginTransaction().add(R.id.login_cardView, f1)
                    .commit(); 
                    status = true;
                }else { 
                    LoginFragment f2 = new LoginFragment();
                    fM.beginTransaction().add(R.id.login_cardView, f2)
                    .commit();
                    status = false;
                } 

            } 
        });

答案 2 :(得分:0)

使用此方法调用片段

.headerStyle 
{
  color: #ffffff;
  font-size: 13px;
  text-align: center;
  font-weight: 500;
  background-color: #07889b;
}

tr:hover:not(.headerStyle):hover 
{
  background-color: #bedcfc;
}