Android Fragment替换活动中的原始内容?

时间:2018-12-23 14:51:01

标签: android android-fragments android-activity

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:id="@+id/mainLayout">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/loginCl1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/loginCl2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="3">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="@id/loginCl1"
            app:layout_constraintStart_toStartOf="@+id/loginCl1"
            app:layout_constraintEnd_toEndOf="@+id/loginCl1"
            app:layout_constraintBottom_toTopOf="@+id/id"
            app:layout_constraintVertical_weight="4"
            android:src="@drawable/logo" />

        <com.fapa.brent.fapa.Common.BrentEditText
            android:id="@+id/id"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:hint="@string/enterid"
            app:layout_constraintBottom_toTopOf="@+id/pw"
            app:layout_constraintEnd_toEndOf="@+id/loginCl1"
            app:layout_constraintStart_toStartOf="@id/loginCl1"
            app:layout_constraintTop_toBottomOf="@+id/image"
            app:layout_constraintVertical_weight="1"/>

        <com.fapa.brent.fapa.Common.BrentEditText
            android:id="@+id/pw"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:hint="@string/enterpw"
            android:inputType="textPassword"
            app:layout_constraintBottom_toBottomOf="@id/loginCl1"
            app:layout_constraintEnd_toEndOf="@+id/loginCl1"
            app:layout_constraintStart_toStartOf="@id/loginCl1"
            app:layout_constraintTop_toBottomOf="@+id/id"
            app:layout_constraintVertical_weight="1"/>
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/loginCl2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/loginCl1"
        app:layout_constraintVertical_weight="2">

        <CheckBox
            android:id="@+id/saveId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/saveId"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <Button
            android:id="@+id/login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/login"
            app:layout_constraintTop_toBottomOf="@+id/saveId"
            app:layout_constraintEnd_toStartOf="@id/signup"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintStart_toStartOf="parent" />

        <Button
            android:id="@+id/signup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/signup"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/saveId"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintStart_toEndOf="@id/login" />

        <Button
            android:id="@+id/findId"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/findid"
            app:layout_constraintEnd_toStartOf="@+id/findPw"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/login" />

        <Button
            android:id="@+id/findPw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/findpw"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/findId"
            app:layout_constraintTop_toBottomOf="@+id/signup" />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.fapa.brent.fapa;

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;
import android.widget.EditText;

import com.facebook.stetho.Stetho;
import com.fapa.brent.fapa.Common.BrentEditText;
import com.fapa.brent.fapa.Encryption.Sha512;
import com.fapa.brent.fapa.Form.FindIdFragment;
import com.fapa.brent.fapa.Form.FindPwFragment;
import com.fapa.brent.fapa.Form.SignUpFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * CREATED BY BRENT ON 2018-12-11
 */

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.id)
    BrentEditText mId;
    @BindView(R.id.pw)
    BrentEditText mPw;
    @BindView(R.id.login)
    Button mBtnLogin;
    @BindView(R.id.signup)
    Button mBtnSignup;
    @BindView(R.id.findId)
    Button mBtnFindId;
    @BindView(R.id.findPw)
    Button mBtnFindPw;

    Fragment mFragment;


    @OnClick(R.id.login)
    void loginValidatation() {
        String id = mId.getText().toString();
        String pw = Sha512.getSHA512(mPw.getText().toString());

    }

    @OnClick({R.id.signup, R.id.findPw, R.id.findId})
    void openUpFragmentByButton(Button button){
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (button.getId()){
            case R.id.signup:
                mFragment = new SignUpFragment();
                fragmentChangeOrAdd(fragmentTransaction, mFragment);
                break;
            case R.id.findId:
                mFragment = new FindIdFragment();
                fragmentChangeOrAdd(fragmentTransaction, mFragment);
                break;
            case R.id.findPw:
                mFragment = new FindPwFragment();
                fragmentChangeOrAdd(fragmentTransaction, mFragment);
                break;
            default:
                break;
        }
    }

    private void fragmentChangeOrAdd(FragmentTransaction fragmentTransaction, Fragment fragment) {
        if(fragmentTransaction == null) {
            fragmentTransaction.add(R.id.mainLayout, fragment);
            fragmentTransaction.commit();
        }else {
            fragmentTransaction.replace(R.id.mainLayout, fragment);
            fragmentTransaction.commit();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Stetho.initializeWithDefaults(this);

    }
}

请查看我的openUpFragmentById()方法。我想做的是,当我单击activity_main.xml上的按钮时,我想给我的一个片段充气并填满整个activity_main,而不显示原始内容。

我知道片段的主要目的是分割或拆分活动,但是我想知道是否可以用我的一个片段中的内容替换(或更改)activity_main.xml中的所有原始内容(我知道某个片段需要viewGroup中的activity_main.xml(以我为例)才能膨胀)。

此外,如果不能的话,我是否应该进行其他活动来应对? 如果可以,请提供有用的链接或建议。预先感谢。

1 个答案:

答案 0 :(得分:1)

我没有看到您的片段xml,但是我遇到了这个问题,我认为您必须将背景设置为您的片段xml父版面

例如:

    <LinearLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentParent"
        android:background="@android:color/white"
>

        <!--your fragment views-->

</LinearLayout>