如何在片段中制作导航抽屉?

时间:2019-07-23 02:50:55

标签: java android

我想制作一个带有3个菜单的底部导航的应用程序,每个菜单都有一个不同的片段(BerandaFragment,JelajahFragment和ProfilkuFragment)。我想将导航抽屉放在菜单“ profilku”中,这就是一个片段(ProfilkuFragment)。这是我的问题。当我们制作导航抽屉时,“控制器”始终位于活动中。我们知道活动和片段是不同的,所以当我将代码从活动移到片段(ProfilkuFragment)时,会有很多错误。帮我解决这个问题。我为我的英语不好对不起,希望大家理解我的意思。谢谢!

我尝试添加导航抽屉活动,并将代码从活动移动到片段。当然还有很多错误。我会尽力解决。是的,这是行不通的

ProfilkuFragment.java

public class ProfilkuFragment extends Fragment {


    public ProfilkuFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_profilku, container, false);
    }

}

MainActivity.java

switch (item.getItemId()) {
                    case R.id.navigation_beranda:
                        BerandaFragment berandaFragment = new BerandaFragment();
                        FragmentTransaction fragmentBerandaTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentBerandaTransaction.replace(R.id.content, berandaFragment);
                        fragmentBerandaTransaction.commit();
                        return true;
                    case R.id.navigation_jelajah:
                        JelajahFragment jelajahFragment = new JelajahFragment();
                        FragmentTransaction fragmentJelajahTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentJelajahTransaction.replace(R.id.content, jelajahFragment);
                        fragmentJelajahTransaction.commit();
                        return true;
                    case R.id.navigation_profilku:
                        ProfilkuFragment profilkuFragment = new ProfilkuFragment();
                        FragmentTransaction fragmentProfilkuTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentProfilkuTransaction.replace(R.id.content, profilkuFragment);
                        fragmentProfilkuTransaction.commit();
                        return true;
                }

我希望当我单击“ profilku”菜单时,PofilkuFragment将与导航抽屉一起出现。

这是我单击profilku菜单时的屏幕截图:

image 1

1 个答案:

答案 0 :(得分:0)

您可以在下面的链接中尝试这个:-

https://gist.github.com/eirabben/46d81bd9db52325563c7

但是根据我个人的建议,不要在侧面菜单中使用导航抽屉,而请在活动中使用它,并且您也可以同时使用底视图。