BottomNavigationBar切换标签页Fragment重复创建

public class IndexActivity extends ActivityBase {
    private Fragment testFg;
    private Fragment homeFg;
    private Fragment indexFg;
    private Fragment alarmFg;
    private Fragment thiFg;
    private FragmentTransaction xTransaction;
    private FragmentManager xFragmentManager;

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
        xFragmentManager = getSupportFragmentManager();
        initView();
        initEvent(this);
    }

    private void initView() {

        if (indexFg == null) {
            indexFg = IndexFg.newInstance();
            new IndexPt((IndexFg) indexFg, getApplicationContext());
        }
        if (testFg == null) {
            testFg = TestFg.newInstance();
            new TestPt((TestFg) testFg, getApplicationContext());
        }
        if (homeFg == null) {
            homeFg = HomeFg.newInstance();
            new HomePt((HomeFg) homeFg, getApplicationContext(), CityDataR.newInstance
                    (getApplicationContext()));
        }
        if (alarmFg == null) {
            alarmFg = AlarmFg.newInstance();
            new AlarmPt((AlarmFg) alarmFg, getApplicationContext());
        }

        xTransaction = xFragmentManager.beginTransaction();
        xTransaction.add(R.id.content_fg, homeFg);
        xTransaction.commit();
        thiFg = homeFg;
    }

    private void initEvent(Activity activity) {
        BottomNavigationBar bottomNavigationBar = findViewById(R.id.index_bnb);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.one, "One"))
                .addItem(new BottomNavigationItem(R.drawable.two, "Two"))
                .addItem(new BottomNavigationItem(R.drawable.one, "Three"))
                .addItem(new BottomNavigationItem(R.drawable.two, "Four"))
                .addItem(new BottomNavigationItem(R.drawable.two, "Five"))
                .initialise();//所有的设置需在调用该方法前完成
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        switchFragment(homeFg);
                        break;
                    case 1:
                        switchFragment(testFg);
                        break;
                    default:
                        switchFragment(indexFg);
                        break;
                }
        }

            @Override
            public void onTabUnselected(int position) {
            }
            @Override
            public void onTabReselected(int position) {
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = XApplicatoion.getRefWatcher(this);
        refWatcher.watch(this);
    }

    private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if (thiFg != fragment) {
            //判断切换的Fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有,则先把当前的Fragment隐藏,把切换的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(thiFg).add(R.id.content_fg, fragment).commit();
            } else {
                //如果已经添加过,则先把当前的Fragment隐藏,把切换的Fragment显示出来
                getSupportFragmentManager().beginTransaction().hide(thiFg).show(fragment).commit();
            }
            thiFg = fragment;
        }
    }

}

 

posted @ 2018-12-19 22:33  XinYiBuFang  阅读(1416)  评论(0编辑  收藏  举报