如何在片段内调用片段并显示到相同的布局

时间:2017-05-03 09:32:36

标签: java android-studio

单击按钮Home时,我在其定义的Framelayout(ID:flayout)中使用底部导航和调用片段(Ex.Home)。我想知道是否可以在片段(Home)中调用另一个片段(例如Buy)并将其显示到Framelayout(ID:flayout)?如何在片段(购买)中找到Framelayout ID,因为它有不同的布局?谢谢!

我可以在底部航行中调用我的其他碎片。这是我的 bottomnavigation.java

的代码
bottomNavigation = (BottomNavigationView) findViewById(R.id.navigation);
    fragmentManager = getSupportFragmentManager();
    bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            switch (id){
                case R.id.navigation_home:
                    fragment = new Home();
                    break;
                case R.id.navigation_buy:
                    fragment = new Buy();
                    break;
                case R.id.navigation_sell:
                    fragment = new Sell();
                    break;
            }
            final FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.content, fragment).commit();
            return true;
        }
    });
    fragment = new Home();
    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content, fragment).commit();

当我去Home()时,在Home()中调用另一个像Buy()的片段。如何访问和显示(R.id.content)Framelayout的Buy(),因为它位于 bottomnavigation.java xml布局中。有可能吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试更改片段:

     getChildFragmentManager().executePendingTransactions();

     final   FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

     transaction.addToBackStack(null);
     transaction.replace(R.id.flayout, new Buy());

     transaction.commit();