首页 > 解决方案 > 如何在导航抽屉中的应用程序启动时检查项目?

问题描述

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

导航抽屉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;
}

标签: androidfragmentnavigation-drawermenuitemchecked

解决方案


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

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

推荐阅读