如何在导航抽屉中的应用启动时检查项目?

时间:2018-11-28 22:32:11

标签: android fragment navigation-drawer menuitem checked

我的导航抽屉中的片段可以正常工作,选择它们时会检查项目。但是,当我打开应用程序时,它会显示导航抽屉的第一项,但是当我打开抽屉项时,直到我触摸它,它才会被选中。

导航抽屉xml:

    <?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">

    <item android:title="Navegador">
        <menu android:checkableBehavior="single">
            <item
                android:id="@+id/nav_inventario"
                android:icon="@drawable/ic_inventario"
                android:checkable="true"
                android:title="Inventario" />
            <item
                android:id="@+id/nav_compras"
                android:icon="@drawable/ic_compras"
                android:checkable="true"
                android:title="Compras" />
            <item
                android:id="@+id/nav_ventas"
                android:icon="@drawable/ic_ventas"
                android:checkable="true"
                android:title="Ventas" />
            <item
                android:id="@+id/nav_ingresos_gastos"
                android:icon="@drawable/ic_ingresos_gastos"
                android:checkable="true"
                android:title="Ingresos-Gastos Extras" />
            <item
                android:id="@+id/nav_reportes"
                android:icon="@drawable/ic_reportes"
                android:checkable="true"
                android:title="Reportes" />
            <item
                android:id="@+id/nav_clientes"
                android:icon="@drawable/ic_clientes"
                android:checkable="true"
                android:title="Clientes" />
            <item
                android:id="@+id/nav_proveedores"
                android:icon="@drawable/ic_proveedores"
                android:checkable="true"
                android:title="Proveedores" />
            <item
                android:id="@+id/nav_agenda"
                android:icon="@drawable/ic_agenda"
                android:checkable="true"
                android:title="Agenda" />
            <item
                android:id="@+id/nav_infominegocio"
                android:icon="@drawable/ic_infominegocio"
                android:checkable="true"
                android:title="Info Mi Negocio" />
        </menu>
    </item>

    <item android:title="MyShop">
        <menu>
            <item
                android:id="@+id/nav_configuracion"
                android:icon="@drawable/ic_configuracion"
                android:title="Configuración" />
            <item
                android:id="@+id/nav_contactar"
                android:icon="@drawable/ic_contactar"
                android:title="Contactar" />
            <item
                android:id="@+id/nav_reportar"
                android:icon="@drawable/ic_reportar"
                android:title="Reportar Fallo" />
        </menu>
    </item>

</menu>

.MainActivity(onNavigationItemSelected):

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    Fragment miFragment = null;
    boolean fragmentSeleccionado = false;

    if (id == R.id.nav_inventario) { // THIS IS THE ITEM SHOWN ON THE APP STARTUP
        miFragment = new InventarioFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.show();

    } else if (id == R.id.nav_compras) {
        miFragment = new ComprasFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.show();

    } else if (id == R.id.nav_ventas) {
        miFragment = new VentasFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.show();

    } else if (id == R.id.nav_ingresos_gastos) {
        miFragment = new IngresosGastosFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.show();

    } else if (id == R.id.nav_reportes) {
        miFragment = new ReportesFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.hide();

    } else if (id == R.id.nav_clientes) {
        miFragment = new ClientesFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.show();

    } else if (id == R.id.nav_proveedores) {
        miFragment = new ProveedoresFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.show();

    } else if (id == R.id.nav_agenda) {
        miFragment = new AgendaFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.hide();

    } else if (id == R.id.nav_infominegocio) {
        miFragment = new InfoMiNegocioFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.hide();

    } else if (id == R.id.nav_configuracion) {
        miFragment = new ConfiguracionFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.hide();

    } else if (id == R.id.nav_contactar) {
        miFragment = new ContactarFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.hide();

    } else if (id == R.id.nav_reportar) {
        miFragment = new ReportarFragment();
        fragmentSeleccionado = true;

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.hide();

    }

    if (fragmentSeleccionado == true) {
        getSupportFragmentManager().beginTransaction().replace(R.id.content_main, miFragment).commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

2 个答案:

答案 0 :(得分:0)

您可以在活动的onCreate方法中执行以下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .
    .
    .
    navigationView.getMenu().getItem(0).setChecked(true);
}

答案 1 :(得分:0)

在导航抽屉xml中:

<item android:title="Navegador">
    <menu android:checkableBehavior="single">
        <item
            android:id="@+id/nav_inventario"
            android:icon="@drawable/ic_inventario"
            android:checkable="true"
            android:checked="true"
            android:title="Inventario" />
        <item
            android:id="@+id/nav_compras"
            android:icon="@drawable/ic_compras"
            android:checkable="true"
            android:title="Compras" /> ...

确保在XML中添加 android:checked =“ true” 或使用代码

navigationView.setCheckedItem(your navigation item id);

希望有帮助! :)