如何从动态数组中打开具有动态菜单列表的导航抽屉中的片段

时间:2019-01-31 10:03:06

标签: android navigation-drawer android-menu

我已经创建了具有来自我的api的动态菜单选项的导航抽屉,现在我想基于从导航抽屉中选择的选项打开片段,我想从导航项中打开主要选项的子选项的片段。 / p>

例如,在我的导航抽屉中,我有一个动态数组,该数组具有四个值1)报告2)分析3)与我们联系4)关于我们

在报告中,当我单击该报告时,要打开该片段,我有一个价值投资组合报告,我该如何实现?我的数组是动态的,主数组具有报表ID,子选项数组具有父ID,如何将其与菜单选项ID进行比较。

This is my code.

    MainActivity.java

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame
                            , new DashboardFragment())
                    .commit();

            initView();
            onClick();


            listData = new ArrayList<>();
            parentIdList = new ArrayList<>();
            reportIdList = new ArrayList<>();
            listSubData = new ArrayList<>();
            reportSubList = new ArrayList<>();

            toolbar.setTitle("Dashboard");
            toolbar.setBackgroundColor(getResources().getColor(R.color.colorLightBlue));
            toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
            setSupportActionBar(toolbar);


            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            //drawer.setDrawerListener(toggle);

            drawerLayout.addDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(MainActivity.this);

    //        ActionBar bar = getSupportActionBar();
    //        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0059b3")));


            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            loginid = sp.getInt("loginId", 0);
            userrole = sp.getInt("userrole", 0);
            clientid = sp.getInt("clientid", 0);
            username = sp.getString("username", "");


            Log.e(" prefs loginId", String.valueOf(loginid));
            Log.e("prefs userrole", String.valueOf(userrole));
            Log.e(" prefs clientid", String.valueOf(clientid));

            if (userrole == 99) {
                tvUserRole.setText("CLIENT");
                tvUserName.setText(username);
            } else {
                tvUserRole.setText("PARTNER");
                tvUserName.setText(username);
            }


            NavigationOptionsData(String.valueOf(userrole), String.valueOf(loginid), "A");

            for(int i=0;i<reportIdList.size();i++)
            {
                if(MenuItemId == reportIdList.get(i))
                {

                    Log.e("Menuitemid=", String.valueOf(MenuItemId));

    //                fragToCall = listData.get(i);
    //                ReportFragment fragment = new ReportFragment();
    //                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    //                fragmentTransaction.replace(R.id.frame, fragment, fragToCall);
    //                fragmentTransaction.add(R.id.test_fragment, fragment, "testtag");
    //
    //                fragmentTransaction.addToBackStack(fragToCall);
    //                fragmentTransaction.commitAllowingStateLoss();
                }
                else
                {
                    Log.e("Not equal", "");
                }
            }

        }

        private void initView() {

            navigationView = findViewById(R.id.nav_view);
            drawerLayout = findViewById(R.id.drawer_layout);
            toolbar = findViewById(R.id.toolbar);
            rvNavOptions = (ListView) findViewById(R.id.rv_nav_data);
            rvNavSubOptions = (ListView) findViewById(R.id.rv_nav_sub_data);

            header = navigationView.getHeaderView(0);
            tvUserName = (TextView) header.findViewById(R.id.tv_user_name_drawer);
            tvUserRole = (TextView) header.findViewById(R.id.tv_user_role_drawer);
            tvOptionsId = (TextView) findViewById(R.id.tv_option_id);


        }




        private void NavigationOptionsData(String roleid, String loginId, String device) {

            final APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
            final NavigationDrawerModel navigationDrawerModel = new NavigationDrawerModel(roleid, loginId, device);
            Call<NavigationDrawerModel> call1 = apiInterface.createNavigationOption(navigationDrawerModel);
            call1.enqueue(new Callback<NavigationDrawerModel>() {
                @Override
                public void onResponse(Call<NavigationDrawerModel> call, Response<NavigationDrawerModel> response) {
                    user1 = response.body();

                    Log.e("Navigation data===", user1.getT0().get(0).getReportText());
                    Log.e("Length of t0====", String.valueOf(user1.getT0().size()));
                    Log.e("Length of t1====", String.valueOf(user1.getT1().size()));

                    for (int i = 0; i < user1.getT0().size(); i++) {


                        String reportText = user1.getT0().get(i).getReportText();
                        Integer reportId = user1.getT0().get(i).getReportId();
                        reportIdList.add(reportId);
                        listData.add(reportText);

                    }

                    for (int i = 0; i < user1.getT1().size(); i++) {

                        Integer reportParentId = user1.getT1().get(i).getParentId();
                        String reportSubName = user1.getT1().get(i).getReportText();
                        parentIdList.add(reportParentId);
                        listSubData.add(reportSubName);
                    }

                    Log.e("Size of parentid list==", String.valueOf(parentIdList.size()));
                    Log.e("Size of reportid list==", String.valueOf(reportIdList.size()));


                    navigationDrawerMenu = new NavigationDrawerMenu(listData, parentIdList, getApplicationContext());
                    rvNavOptions.setAdapter(navigationDrawerMenu);


                }

                @Override
                public void onFailure(Call<NavigationDrawerModel> call, Throwable t) {

                    Log.e("Error is==", t.getMessage());
                }


            });
        }


        private void onClick() {

            rvNavOptions.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                    navigationDrawerMenu.onClick(view);
                    MenuItemId = (int) id;
                    Log.e("id of item click", String.valueOf(id));


                }
            });

        }


        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            int id = menuItem.getItemId();
            Log.e("Menu item id is=", String.valueOf(id));
            return true;
        }

我将menuitemid与reportidlist项进行了比较,这是正确的吗?

0 个答案:

没有答案