使用FrameLayout显示Fragment两个页切换

布局用一个




    


主页面用


public class MainActivity extends AppCompatActivity {

    @BindView(R.id.mFrame)
    FrameLayout mFrame;
    private LoginFragment loginFragment;
    private RegisterFragment registerFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        loginFragment = new LoginFragment();
        registerFragment = new RegisterFragment();
        //事务管理
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        transaction.add(R.id.mFrame,loginFragment).show(loginFragment);//默认显示第一个Frament
        transaction.add(R.id.mFrame,registerFragment).hide(registerFragment);//添加并隐藏
        transaction.commit();
    }
    //切换Fragment到注册页
    public void loginTogister(){
       getSupportFragmentManager()
               .beginTransaction()
               .show(registerFragment).hide(loginFragment).commit();

    }
    //切换到注册
    public void regisTologin(){
        getSupportFragmentManager()
                .beginTransaction()
                .show(loginFragment).hide(registerFragment).commit();
    }

    public void destoryFinsh(){
        finish();
    }

}

在第一个Fragment里隐藏当前的Frament显示第二个Fragment

public class LoginFragment extends Fragment {
    @BindView(R.id.tv_login_goRegister)
    TextView tvLoginGoRegister;
    @BindView(R.id.tv_login_goLogin)
    Button tvLoginGoLogin;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 点击切换到注册页
     */
    @OnClick(R.id.tv_login_goRegister)
    public void onClick() {
        ((MainActivity) getActivity()).loginTogister();
    }
}

在第二个Fragment里隐藏当前的Frament显示第一个Fragment

public class register extends Fragment {
    @BindView(R.id.tv_login_goRegister)
    TextView tvLoginGoRegister;
    @BindView(R.id.tv_login_goLogin)
    Button tvLoginGoLogin;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 点击切换到注册页
     */
    @OnClick(R.id.tv_login_goRegister)
    public void onClick() {
     ((MainActivity) getActivity()).regisTologin();
    }
}

你可能感兴趣的:(Android,开发)