View view switches behave incorrectly

I follow this color scheme of navigation. enter image description here

I created 3 selectors, for background, text and icon. When I installed the selector, my navigation view looked like this: enter image description here

The problem is that no element is displayed, and if I click one element, the background color of all elements changes to gray. The icon never gets the primary color.

Here is the XML:

 <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            app:itemTextColor="@drawable/nav_item_text"
            app:itemIconTint="@drawable/nav_item_icon_tint"
            app:itemBackground="@drawable/nav_item_background"
            app:headerLayout="@layout/drawer_student_data_header"
            app:menu="@menu/drawer"
            android:id="@+id/navigationView"/>

nav_item_icon_tint.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/gray_600"/>
    <item android:state_selected="true" android:color="@color/primary" />
    <item android:color="@color/gray_600" />
</selector>

nav_item_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/black_87_percent"/>
    <item android:state_selected="true" android:color="@color/primary" />
    <item android:color="@color/black_87_percent" />
</selector>

nav_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/gray_400"/>
    <item android:state_selected="true" android:drawable="@color/gray_200" />
    <item android:drawable="@android:color/white" />
</selector>
+2
source share
3 answers

, , - android:state_checked android:state_selected ( , ).

+1

setItemTextColor setItemIconTintList NavigationDrawer,

, onCreateView:

 int[][] states = new int[][]{
        new int[]{-android.R.attr.state_checked},// unchecked state
        new int[]{android.R.attr.state_checked}, // checked state
};

int[] colors = new int[]{
        ContextCompat.getColor(this, R.color.colorPrimary),
        ContextCompat.getColor(this, R.color.colorPrimaryDark)
};

ColorStateList colorStateList = new ColorStateList(states, colors);
navigationView.setItemTextColor(colorStateList);
navigationView.setItemIconTintList(colorStateList);

. .

. , . Color . developer.android.

0

Make sure that you are not accessing the menu item from the bottom navigation and set its status to "checked" twice. To avoid this, use:

mBottomNavigationView.selectedItemId(R.id.your_menu_item_id);

instead

mBottomNavigationView.menu.getItem(position).setChecked(true);
0
source

Source: https://habr.com/ru/post/1622637/


All Articles