Comment changer la couleur d'arrière-plan de l'élément sélectionné dans le tiroir de navigation


Manmeet Singh |

Je travaillais sur le tiroir Nav sur mon application. J'ai fait le fond de mon tiroir de navigation # 000000 (noir). Chaque fois que je sélectionne un élément, la couleur du texte du texte passe de # E44F50 (rouge carotte) au noir mais je ne peux pas changer la couleur d'arrière-plan de l'élément sélectionné. Tout ce que je veux, c'est changer la couleur d'arrière-plan de l'article sélectionné du noir au rouge carotte.

Voici le lien github vers mon application-

https://github.com/manmeet-22/NewsSnips-app.git

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimary"
        app:headerLayout="@layout/header"
        app:itemIconTint="@drawable/drawer_item" //changing text color 
        app:itemTextAppearance="@style/TextAppearance20"
        app:itemTextColor="@color/drawer_item"
        app:itemBackground="@drawable/drawer_selected_item" //trying to change backgroundcolor the same way
        app:menu="@menu/drawer"
        tools:context="com.androidexample.newssnips.app.NavigationDrawerFragment"/>
</android.support.v4.widget.DrawerLayout>

En cela, j'ai essayé de changer la couleur d'arrière-plan comme je l'ai fait pour le texte, mais mon application se bloque.

voici mon tiroir_selected_item.xml

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

voici mon tiroir_item.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/colorPrimary" android:state_checked="true" />
        <item android:color="@color/colorAccent" />
</selector>

Voici mon MainActivity.java:

navigationView = (NavigationView) findViewById(R.id.navigation_view);
        navigationView.setItemIconTintList(null);
        Menu menu = navigationView.getMenu();

        //For chnaging the textColor and textSize of group's Name ie. Category
        MenuItem categoryName= menu.findItem(R.id.categoryGroup);
        SpannableString s = new SpannableString(categoryName.getTitle());
        s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance25), 0, s.length(), 0);

        //For changing the Text of action bar as the selected category
        categoryName.setTitle(s);

        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked())
                    menuItem.setChecked(false);
                else
                    menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();

                //Check to see which item was being clicked and open the Appopriate news accordingly
                switch (menuItem.getItemId()) {

                    case R.id.itemWorld:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=world";
                        setTitle("World News");

                        basicDefaults();
                        return true;
                    case R.id.itemFootball:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=football";
                        setTitle("Football News");
                        basicDefaults();
                        return true;
                    case R.id.itemFashion:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=fashion";
                        setTitle("Fashion News");
                        basicDefaults();
                        return true;
                    case R.id.itemSports:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=sport";
                        setTitle("Sports News");
                        basicDefaults();
                        return true;
                    case R.id.itemBusiness:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=business";
                        setTitle("Business News");
                        basicDefaults();
                        return true;
                    case R.id.itemTechnology:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=technology";
                        setTitle("Technology News");
                        basicDefaults();
                        return true;
                    case R.id.itemOpinion:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=commentisfree";
                        setTitle("Opinions");
                        basicDefaults();
                        return true;
                    case R.id.itemCulture:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=culture";
                        setTitle("Culture News");
                        basicDefaults();
                        return true;
                    case R.id.itemTravel:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=travel";
                        setTitle("Travel News");
                        basicDefaults();
                        return true;
                    case R.id.itemLifestyle:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=lifeandstyle";
                        setTitle("LifeStyle News");
                        basicDefaults();
                        return true;
                    case R.id.itemEnvironment:
                        GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4&section=environment";
                        setTitle("Environment News");
                        basicDefaults();
                        return true;

                    default:
                        Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                        return true;

                }
            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

                super.onDrawerOpened(drawerView);

            }
        };

        //calling sync state is necessay or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();
    }

PS.- J'ai essayé d'implémenter d'autres méthodes comme

l'ajout de ceci dans apptheme dans le fichier styles.xml.

<item name="android:activatedBackgroundIndicator">@drawable/drawer_selected_item</item>

Mais je ne trouve toujours pas ma solution.
Aidez-moi, c'est la dernière chose qui reste dans mon projet.

Manmeet Singh |

J'ai la solution

Dans NavigationView, j'ai fait-

        app:itemBackground="@drawable/drawer_selected_item"

Ainsi mon NavigationView ressemble à-

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/colorPrimary"
            app:headerLayout="@layout/header"
            app:itemIconTint="@drawable/drawer_item"
            app:itemTextAppearance="@style/TextAppearance20"
            app:itemTextColor="@drawable/drawer_item"
            app:menu="@menu/drawer"
            app:itemBackground="@drawable/drawer_selected_item"
tools:context="com.androidexample.newssnips.app.NavigationDrawerFragment" />

Dans le tiroir_élément sélectionné, j'ai modifié le fichier comme-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@color/colorAccent"/>
<item android:drawable="@android:color/transparent" />
</selector>

Articles connexes