Как проверить элемент при запуске приложения в Навигаторе? - PullRequest
0 голосов
/ 29 ноября 2018

У меня есть навигационный ящик с отлично работающими фрагментами, и элементы выбираются, когда я их выбираю.Но когда я открываю свое приложение, оно показывает первый элемент Навигационного Ящика, но когда я открываю ящик, элемент не проверяется, пока я не коснусь его.

Ящик навигации 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>

. Основная активность (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 голосов
/ 29 ноября 2018

В вашем Навигационном Ящике 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" /> ...

Убедитесь, что вы добавили android: checked = "true" в ваш XML или код

navigationView.setCheckedItem(your navigation item id);

Надеюсь, это поможет!:)

0 голосов
/ 29 ноября 2018

Вы можете сделать это в методе onCreate своей деятельности примерно так:

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