fragment的切换

package fan.fragmentdemo;

import android.os.Bundle;
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.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import fan.fragmentdemo.fragment.OneFragment;
import fan.fragmentdemo.fragment.ThreeFragment;
import fan.fragmentdemo.fragment.TwoFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //当前显示的fragment
    private static final String STATE_FRAGMENT_SHOW = "STATE_FRAGMENT_SHOW";

    private TextView tvone;
    private TextView tvtwo;
    private TextView tvthree;
    private FragmentManager fragmentManager;
    private OneFragment fgOne;
    private TwoFragment fgTwo;
    private ThreeFragment fgThree;

    private Fragment currentFragment = new Fragment();

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

        this.tvthree = (TextView) findViewById(R.id.tv_three);
        this.tvtwo = (TextView) findViewById(R.id.tv_two);
        this.tvone = (TextView) findViewById(R.id.tv_one);

        fragmentManager = getSupportFragmentManager();

        tvthree.setOnClickListener(this);
        tvtwo.setOnClickListener(this);
        tvone.setOnClickListener(this);


        if (savedInstanceState != null) { // “内存重启”时调用

            //获取“内存重启”时保存的fragment名字
            String saveName = savedInstanceState.getString(STATE_FRAGMENT_SHOW);

            //从fragmentManager里面找到fragment
            fgOne = (OneFragment) fragmentManager.findFragmentByTag(OneFragment.class.getName());
            fgTwo = (TwoFragment) fragmentManager.findFragmentByTag(TwoFragment.class.getName());
            fgThree = (ThreeFragment) fragmentManager.findFragmentByTag(ThreeFragment.class.getName());

            //如果为空就默认操作
            if(TextUtils.isEmpty(saveName)){
                //解决重叠问题
                fragmentManager.beginTransaction()
                        .show(fgOne)
                        .hide(fgTwo)
                        .hide(fgThree)
                        .commit();

                //把当前显示的fragment记录下来
                currentFragment = fgOne;

            }else{

                if(saveName.equals(fgOne.getClass().getName())){    //如果推出之前是OneFragment

                    //解决重叠问题
                    fragmentManager.beginTransaction()
                            .show(fgOne)
                            .hide(fgTwo)
                            .hide(fgThree)
                            .commit();

                    //把当前显示的fragment记录下来
                    currentFragment = fgOne;

                }else if(saveName.equals(fgTwo.getClass().getName())){  //如果推出之前是TwoFragment

                    //解决重叠问题
                    fragmentManager.beginTransaction()
                            .show(fgTwo)
                            .hide(fgOne)
                            .hide(fgThree)
                            .commit();

                    //把当前显示的fragment记录下来
                    currentFragment = fgTwo;

                }else{    //如果推出之前是ThreeFragment

                    //解决重叠问题
                    fragmentManager.beginTransaction()
                            .show(fgThree)
                            .hide(fgTwo)
                            .hide(fgOne)
                            .commit();

                    //把当前显示的fragment记录下来
                    currentFragment = fgThree;

                }

            }


        }else{      //正常启动时调用

            fgOne = new OneFragment();
            fgTwo = new TwoFragment();
            fgThree = new ThreeFragment();

            showFragment(fgOne);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        //“内存重启”时保存当前的fragment名字
        outState.putString(STATE_FRAGMENT_SHOW,currentFragment.getClass().getName());
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.tv_one:

                showFragment(fgOne);

                break;
            case R.id.tv_two:

                showFragment(fgTwo);

                break;
            case R.id.tv_three:

                showFragment(fgThree);

                break;

        }

    }
//
//    /**
//     * 使用replace切换页面
//     * 显示fragment
//     */
//    private void showFragment(Fragment fg){
//
//        FragmentTransaction transaction = fragmentManager.beginTransaction();
//        transaction.replace(R.id.content, fg);
//        transaction.commit();
//
//    }

//
//    /**
//     * 使用show() hide()切换页面
//     * 显示fragment
//     */
//    private void showFragment(Fragment fg){
//
//        FragmentTransaction transaction = fragmentManager.beginTransaction();
//
//        //如果之前没有添加过
//        if(!fg.isAdded()){
//            transaction
//                    .hide(currentFragment)
//                    .add(R.id.content,fg);
//        }else{
//            transaction
//                    .hide(currentFragment)
//                    .show(fg);
//        }
//
//        currentFragment = fg;
//
//        transaction.commit();
//
//    }

    /**
     * 使用show() hide()切换页面
     * 显示fragment
     */
    private void showFragment(Fragment fg){

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        //如果之前没有添加过
        if(!fg.isAdded()){
            transaction
                    .hide(currentFragment)
                    .add(R.id.content,fg,fg.getClass().getName());  //第三个参数为添加当前的fragment时绑定一个tag,即绑定fragment的类名
        }else{
            transaction
                    .hide(currentFragment)
                    .show(fg);
        }

        currentFragment = fg;

        transaction.commit();

    }
}

你可能感兴趣的:(fragment的切换)