使用底栏Android切换片段(显示/隐藏)

时间:2017-01-25 02:44:43

标签: android android-fragments

我的应用程序上的实现片段和底栏有问题..

我想根据初始化底栏切换片段.. 这是我的代码

    private Fragment frag_summary = new SummaryFragment();
    private Fragment frag_my_proposal = new MyProposalFragment();
    private Fragment frag_proposal = new ProposalFragment();
    private Fragment frag_friends = new FriendsFragment();

bottomNavigation.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                setSelectedFragment(tabId);
            }
        });


    public void setSelectedFragment(int id) {
        switch (id) {
            case R.id.menu_home:
                setFragment(id, frag_summary);
                break;
            case R.id.menu_my_proposals:
                setFragment(id, new MyProposalFragment());
                break;
            case R.id.menu_proposal:
                setFragment(id, new ProposalFragment());
                break;
            case R.id.menu_friends:
                setFragment(id, new FriendsFragment());
                break;
            case R.id.menu_more:
               /* drawer.openDrawer(GravityCompat.START);
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    drawer.openDrawer(GravityCompat.START);
                }*/
                break;
            default:
                setFragment(id, frag_summary);
                break;

        }
    }

    private void setFragment(int id, Fragment fragments) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        String fragmentTag = String.valueOf(id);

        if (frag_summary.isAdded()) {
            fragmentTransaction.hide(frag_summary);
        }
        if (frag_my_proposal.isAdded()) {
            fragmentTransaction.hide(frag_my_proposal);
        }
        if (frag_proposal.isAdded()) {
            fragmentTransaction.hide(frag_proposal);
        }
        if (frag_friends.isAdded()) {
            fragmentTransaction.hide(frag_friends);
        }
        if (fragments.isAdded()) {
            fragmentTransaction.show(fragments);
        } else {
            fragmentTransaction.add(R.id.content_frame, fragments, fragmentTag);
        }

        fragmentTransaction.commitAllowingStateLoss();


    }

问题是:当我从Fragment frag_summary中选择Fragment frag_friends时,Fragment frag_summary和其他片段有时仍会显示..就像其他活动一样:从Fragment frag_friends到Fragment frag_proposal。

如何修复它? 对不起我的英文

1 个答案:

答案 0 :(得分:0)

使用 -

重新计算setFragment中的代码
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragments, fragmentTag);
fragmentTransaction.commitAllowingStateLoss();

基本上你正在替换当前的可见片段。

编辑:更改以下代码 -

public void setSelectedFragment(int id) {
        switch (id) {
            case R.id.menu_home:
                setFragment(id, frag_summary);
                break;
            case R.id.menu_my_proposals:
                setFragment(id, /*send the class instance you created for this fragment */);
                break;
            case R.id.menu_proposal:
                setFragment(id, /*send the class instance you created for this fragment */);
                break;
            case R.id.menu_friends:
                setFragment(id, /*send the class instance you created for this fragment */);
                break;
            case R.id.menu_more:
               /* drawer.openDrawer(GravityCompat.START);
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    drawer.openDrawer(GravityCompat.START);
                }*/
                break;
            default:
                setFragment(id, frag_summary);
                break;

        }
    }