• 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;
            }
        }
    
    }
  • 相关阅读:
    调研当前大学生的三个痛点
    作业-- 统计文本文件中的字符数、单词数、行数
    我的课程表--项目需求分析
    Android随机生成四则运算
    校友信息管理&SNS互动平台之前言、目录及说明
    校友信息管理&SNS互动平台之技术框架选择
    校友信息管理系统&SNS互动平台之用户需求及概要设计
    WordPress文件上传与下载问题解决
    oc - NSArray基础用法总结
    AutoLayout 使用详细
  • 原文地址:https://www.cnblogs.com/xinyibufang/p/10146618.html
Copyright © 2020-2023  润新知