简体   繁体   English

在Tabs Android中添加片段的最佳方法是什么

[英]What is the best way to add fragment in the Tabs android

My application had a bottom navigation bar which has 5 tabs. 我的应用程序有一个底部导航栏,其中有5个标签。 So according to these tabs, I have 5 fragments When I click on the tab, the fragment changed according to that tab. 因此,根据这些选项卡,我有5个片段当我单击选项卡时,片段会根据该选项卡进行更改。 I can switch fragment by using the method beginTransaction().replace... I dont want the fragment to be destroyed and recreated again each time I switch tabs, so my solution is sth like this 我可以使用beginTransaction()。replace方法来切换片段。我不希望每次切换标签时都破坏片段并重新创建,所以我的解决方案是这样的

//I init 5 fragments
Fragment1 frag1 = new Fragment1();
Fragment2 frag2 = new Fragment2();
Fragment3 frag3 = new Fragment3();
Fragment4 frag4 = new Fragment4();
Fragment5 frag5 = new Fragment5();

//When I click on tab, for example tab1, I hide all fragments except tab1
//hide all fragments
getSupportFragmentManager()
                    .beginTransaction()
                    .hide(fragment1) //Fragment2, 3, 4, 5 as well
                    .commit();

//show fragment 1
getSupportFragmentManager()
                    .beginTransaction()
                    .show(fragment1)
                    .commit();

It works very well, but the problem is sometimes 2 fragments show at once time (I dont know why because I hide all fragments) Any other way to achieve that? 它工作得很好,但问题是有时一次显示2个片段(我不知道为什么,因为我隐藏了所有片段)还有其他方法可以实现吗? Switch fragment without destroying it and creating it again. 切换片段而不破坏它并再次创建它。

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. 希望对您有所帮助,并在整个项目中使用此方法替换片段。

Using ViewPager with FragmentPagerAdapter suits for you in this case. 在这种情况下,适合将ViewPagerFragmentPagerAdapter一起使用。

Then use ViewPager#setOffsetPageLimit(5) . 然后使用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. 让我们尝试一下,然后告诉我您的问题是否解决。 ;) ;)

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

} }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM