简体   繁体   English

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

[英]how to make a navigation drawer inside fragment?

I want to make an app that has a bottom navigation with 3 menu, and each menu has a different fragment (BerandaFragment, JelajahFragment, and ProfilkuFragment). 我想制作一个带有3个菜单的底部导航的应用程序,每个菜单都有一个不同的片段(BerandaFragment,JelajahFragment和ProfilkuFragment)。 I want to put a navigation drawer in menu "profilku" which is thats a fragment (ProfilkuFragment). 我想将导航抽屉放在菜单“ profilku”中,这就是一个片段(ProfilkuFragment)。 And this is my problem. 这是我的问题。 When we make a navigation drawer the "controller" is always inside an activity. 当我们制作导航抽屉时,“控制器”始终位于活动中。 As we know that activity and fragment are different, so when I move the code from activity to fragment (ProfilkuFragment) there so much error. 我们知道活动和片段是不同的,所以当我将代码从活动移到片段(ProfilkuFragment)时,会有很多错误。 help me to solve this. 帮我解决这个问题。 I am sorry for my bad English I hope you guys understand what I mean. 我为我的英语不好对不起,希望大家理解我的意思。 Thank you! 谢谢!

I've tried to add a navigation drawer activity and move the code from activity to fragment. 我试图添加一个导航抽屉活动并将代码从活动移动到片段。 And of course there so much error. 当然还有很多错误。 I fix it as best I can. 我会尽力解决。 And yeah that's not work 是的,这是行不通的

ProfilkuFragment.java 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 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;
                }

I expect when I click "profilku" menu, PofilkuFragment will appear with a navigation drawer. 我希望当我单击“ profilku”菜单时,PofilkuFragment将与导航抽屉一起出现。

this is screenshot when i click profilku menu: 这是我单击profilku菜单时的屏幕截图:

图片1

You can try this below link:- 您可以尝试以下链接:

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

But as per my personal suggestion do not use navigation drawer in side menu, instead of please use this in activity and with that you can use bottom view also at that same time. 但是根据我个人的建议,不要在侧面菜单中使用导航抽屉,而请在活动中使用它,并且您也可以同时使用底视图。

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

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