繁体   English   中英

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

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

我的应用程序有一个底部导航栏,其中有5个标签。 因此,根据这些选项卡,我有5个片段当我单击选项卡时,片段会根据该选项卡进行更改。 我可以使用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();

它工作得很好,但问题是有时一次显示2个片段(我不知道为什么,因为我隐藏了所有片段)还有其他方法可以实现吗? 切换片段而不破坏它并再次创建它。

为了添加片段,我为我的项目编写了这段代码,希望对您有所帮助。

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


    }

希望对您有所帮助,并在整个项目中使用此方法替换片段。

在这种情况下,适合将ViewPagerFragmentPagerAdapter一起使用。

然后使用ViewPager#setOffsetPageLimit(5) 这将帮助您显示/隐藏片段,而无需再次创建。

遵循本教程

让我们尝试一下,然后告诉我您的问题是否解决。 ;)

您不必隐藏片段,只需像下面的方法那样替换片段即可:

public void setFragment(Fragment fragmentWhichYouWantToShow) {

        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(R.id.container, fragmentWhichYouWantToShow);
        ft.commit();

}

尝试以单笔交易进行交易。

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