简体   繁体   English

如何改进隐藏/显示NavigationView菜单项的方法?

[英]How to Improve the way to Hide/Show NavigationView menu Items?

I was wondering how can I Improve the way that I'm handling menu item access in my app. 我想知道如何改进我在我的应用程序中处理菜单项访问的方式。

First of all I've a Login Activity where the users logs in and if succes it retrieves a list of all the menu items he can have access. 首先,我有一个用户登录的登录活动,如果成功,它会检索他可以访问的所有菜单项的列表。

Then in every activity I need to run the method to hide the menu items that the user have not access. 然后在每个活动中,我需要运行该方法来隐藏用户无法访问的菜单项。

Its called setMenus and the List listaMenuMuestra is the list obtained in the Login Activity 其名为setMenus和List listaMenuMuestra是在Login Activity中获得的列表

public void setMenus(List<ListaMenu> listaMenuMuestra){
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        Menu nav_menu = navigationView.getMenu();
        for(int i = 0; i < listaMenuMuestra.size(); i++){
            try{
                int idMenu = getResources().getIdentifier(listaMenuMuestra.get(i).getIdMenu(), "id", getPackageName());
                nav_menu.findItem(idMenu).setVisible(true);
            }catch (Exception ex){
                Log.d("error","error");
            }
        }

Here's my activity_main_drawer.xml . 这是我的activity_main_drawer.xml By default all the items are invisible. 默认情况下,所有项目都是不可见的。

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_inicio"
            android:icon="@drawable/ic_menu_home"
            android:title="Inicio"
            android:visible="false"/>

        <item
            android:id="@+id/nav_ultimas"
            android:icon="@drawable/ic_menu_send"
            android:title="Últimas consultas"
            android:visible="false"/>

    </group>
    <item android:title="Estadísticas"
        android:id="@+id/nav_tituloestadisticas"
        android:icon="@drawable/ic_insert_chart">
        <menu>
            <item
                android:id="@+id/nav_torta"
                android:icon="@drawable/ic_pie_chart"
                android:title="Gráfico de torta"
                android:visible="false"/>
            <item
                android:id="@+id/nav_lineal"
                android:icon="@drawable/ic_show_chart"
                android:title="Análisis por fechas"
                android:visible="false"/>
        </menu>
    </item>


</menu>

The "problem" that Im facing is that I've plenty Activities and it seems pretty odd to me to have to run setMenus on each Activity. 我面临的“问题”是我有很多活动,对我来说,在每个Activity上运行setMenus似乎很奇怪。

So the questions are Is there a better way to handle menu items access with multiple activies and NavigationDrawer? 所以问题是有没有更好的方法来处理多个活动和NavigationDrawer的菜单项访问? Can just setMenus be called only once and keep these changes across all activities? 只能调用setMenus一次并在所有活动中保留这些更改吗?

Why not make a custom activity that extends to AppCompatActivity and override onResume() method to call your setMenus() . 为什么不进行扩展到AppCompatActivity的自定义活动并覆盖onResume()方法来调用setMenus()

class MenuCheckerActivty: AppCompatActivity() {

  override fun onResume(){
     super.onResume()
     setMenus(listaMenuMuestra)
  }
}

Now in your normal activity, extend it to the MenuCheckerActivity created above. 现在,在您的正常活动中,将其扩展到MenuCheckerActivity创建的MenuCheckerActivity

class MainActivtity: MenuCheckerActivity {

}

Since we have overridden the onResume() of MenuCheckerActivity , the setMenus() function will be called whenever super.onResume() is called. 由于我们已经覆盖了MenuCheckerActivityonResume()MenuCheckerActivity只要调用super.onResume()就会调用setMenus()函数。 Haven't tested the code but pretty sure it clarifies the concept. 没有测试过代码,但很确定它澄清了这个概念。 Hope it helps. 希望能帮助到你。

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

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