底部导航加载碎片是缓慢的

时间:2017-12-05 09:54:02

标签: android android-fragments bottomnavigationview

我在我的项目中使用BottomNavigationView,当我按下一个项目并加载例如My Ticket片段生成QR码并需要一段时间,在选择项目之前会有很大的延迟。我试图将AsyncTask加载到片段中的onResume()和onActivityCreated()中的所有数据,但它并没有很好地工作。

如何顺利使用底部导航视图和碎片?

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_view);

    Menu test = bottomNavigationView.getMenu();
    test.add(Menu.NONE, 1, Menu.NONE, "Home").setIcon(R.drawable.ic_action_home);
    test.add(Menu.NONE, 2, Menu.NONE, "Agenda").setIcon(R.drawable.ic_action_event);
    test.add(Menu.NONE, 3, Menu.NONE, "Ticket").setIcon(R.drawable.ic_action_credit_card);
    test.add(Menu.NONE, 4, Menu.NONE, "Profile").setIcon(R.drawable.ic_action_person);
    test.add(Menu.NONE, 5, Menu.NONE, "More").setIcon(R.drawable.ic_action_more);

    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            Fragment selectedFragment = null;

            switch (item.getItemId()) {
                case 1:
                    if(menuFrame.getVisibility() == View.VISIBLE) {
                        closeBottomMenu();
                    }
                    selectedFragment = HomeFragment.newInstance();
                    break;
                case 2:
                    if(menuFrame.getVisibility() == View.VISIBLE) {
                        closeBottomMenu();
                    }
                    selectedFragment = AgendaFragment.newInstance();
                    break;
                case 3:
                    if(menuFrame.getVisibility() == View.VISIBLE) {
                        closeBottomMenu();
                    }
                    selectedFragment = TicketFragment.newInstance();

                    break;
                case 4:
                    if(menuFrame.getVisibility() == View.VISIBLE) {
                        closeBottomMenu();
                    }
                    selectedFragment = ProfileFragment.newInstance();
                    break;
                case 5:
                    if(menuFrame.getVisibility() == View.INVISIBLE) {
                        openBottomMenu();
                    } else {
                        closeBottomMenu();
                    }
                    break;
            }

            if(selectedFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content_main, selectedFragment);
                transaction.commit();
            }

            return true;
        }
    });

0 个答案:

没有答案