Bottom Navigation loading Fragments is SLOW

非 Y 不嫁゛ 提交于 2020-01-02 03:17:09

问题


I'm using a BottomNavigationView in my project and when I press an item and load e.g. My Ticket fragment, which generates a QR-code and takes a while, there is a huge delay until the item is selected. I tried to put an AsyncTask to load all data in onResume() and onActivityCreated() in the Fragment but it didn't work well.

How can I use Bottom Navigation View and Fragments smoothly?

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;
        }
    });

来源:https://stackoverflow.com/questions/47650599/bottom-navigation-loading-fragments-is-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!