What is the best way to add fragment in the Tabs android

后端 未结 4 619
南方客
南方客 2021-01-17 00:17

My application had a bottom navigation bar which has 5 tabs. So according to these tabs, I have 5 fragments When I click on the tab, the fragment changed according to that t

相关标签:
4条回答
  • 2021-01-17 00:45

    you don't have to need to hide the fragment just replace the fragment like this method:

    public void setFragment(Fragment fragmentWhichYouWantToShow) {
    
            fm = getSupportFragmentManager();
            ft = fm.beginTransaction();
            ft.replace(R.id.container, fragmentWhichYouWantToShow);
            ft.commit();
    

    }

    0 讨论(0)
  • 2021-01-17 00:46

    Using ViewPager with FragmentPagerAdapter suits for you in this case.

    Then use ViewPager#setOffsetPageLimit(5). This will help you show/hide your fragments without recreating it again.

    Follow this tutorial

    Let try it, then tell me if your problem is solved or not. ;)

    0 讨论(0)
  • 2021-01-17 00:51

    for adding fragment I make this code for my project, hope it will be help.

     public static void replaceFragment(Fragment fragment, FragmentManager fragmentManager) {
            String backStateName = fragment.getClass().getName();
            String fragmentTag = backStateName;
    
    
            Fragment currentFrag = fragmentManager.findFragmentById(R.id.frame_container);
            Log.e("Current Fragment", "" + currentFrag);
    
    //        boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
            int countFrag = fragmentManager.getBackStackEntryCount();
            Log.e("Count", "" + countFrag);
    
    
            if (currentFrag != null && currentFrag.getClass().getName().equalsIgnoreCase(fragment.getClass().getName())) {
                return;
            }
    
    
            FragmentTransaction ft = fragmentManager.beginTransaction();
    //        if (!fragmentPopped) {
    
            ft.replace(R.id.frame_container, fragment);
            ft.addToBackStack(backStateName);
            ft.commit();
    //        }
    
            currentFrag = fragmentManager.findFragmentById(R.id.frame_container);
            Log.e("Current Fragment", "" + currentFrag);
    
    
        }
    

    hope this will be help you, and use this method in entire project for replacing fragment.

    0 讨论(0)
  • 2021-01-17 00:51

    Try to make it in a singe transaction.

    protected void showAsRootFragment(Fragment fragment, @NonNull String tag) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();        
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        if (supportFragmentManager.getFragments() != null) {
            for (Fragment attachedFragment : supportFragmentManager.getFragments()) {
                if (attachedFragment != null && !tag.equals(attachedFragment.getTag())) {
                    transaction.hide(attachedFragment);
                    attachedFragment.setUserVisibleHint(false);
                }
            }
        }
    
        if (!fragment.isAdded()) {
            transaction.add(R.id.frame_container, fragment, tag);
            fragment.setUserVisibleHint(true);
        } else {
            transaction.show(fragment);
            fragment.setUserVisibleHint(true);
        }
    
        transaction.commit();
    }    
    
    0 讨论(0)
提交回复
热议问题