Autore Topic: Background color item selezionato navigation drawer  (Letto 417 volte)

Offline lucifer

  • Nuovo arrivato
  • *
  • Post: 2
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    samsung s3
  • Sistema operativo:
    Ubuntu 12.14 LTS
Background color item selezionato navigation drawer
« il: 12 Novembre 2014, 18:18:27 CET »
0
Allora ragazzi ho un problema, il mio navigation drawer funziona correttamente dal punto di vista logico, ovvero carica i fragment giusti in base alla voce selezionata nel menu.

Il mio PROBLEMA è di tipo grafico, ovvero non riesco a modificare il colore di sfondo dell'item selezionato che io vorrei mettere oro (#ffd700), ed invece è sempre il classico azzurro di partenza.
Ovvero l'item diventa oro solo nell'istante in cui premo la voce del menu desiderata, per poi sparire subito in quanto viene caricato il fragment desiderato. Se poi ripremo l'icona in alto a sx per visualizzare il drawer navigation l'item selezionato è nuovamente azzurro e NON oro.
Come devo fare per visualizzarlo oro?

Vi posto qui i 3 .xml in questione e anche il file MainActivity.java

Vi ringrazio infinitamente per il suggerimento!

activity_main: layout contenente il drawer navigator, faccio uso del selector.
activity_main.xml
Codice (XML): [Seleziona]
<android.support.v4.widget.DrawerLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/drawer_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <FrameLayout
       android:id="@+id/content_frame"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       />
    <ListView
       android:id="@+id/left_drawer"
       android:layout_width="240dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:choiceMode="singleChoice"
       android:divider="@android:color/transparent"
       android:dividerHeight="0dp"
       android:listSelector="@drawable/background_activated"
       android:background="#111"/>
</android.support.v4.widget.DrawerLayout>


background_activated: selector che seleziona il colore oro per ogni stato.
background_activated.xml
Codice (XML): [Seleziona]
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- le provo tutte -->
    <item android:state_activated="true" android:drawable="@color/temagold_color" />
    <item android:state_selected="true" android:drawable="@color/temagold_color" />
    <item android:state_pressed="true" android:drawable="@color/temagold_color" />
    <item android:state_focused="true" android:drawable="@color/temagold_color" />
    <item android:state_enabled="true" android:drawable="@color/temagold_color" />
    <item android:state_checked="true" android:drawable="@color/temagold_color" />
    <item android:state_window_focused="true" android:drawable="@color/temagold_color" />

    <item android:drawable="@color/temagold_color" />
</selector>


temagold_color: qui specifico il colore oro
temagold_color.xml
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- color oro -->
    <color name="temagold_color">#ffd700</color>
</resources>

MainActivity.java
Codice (Java): [Seleziona]
public class MainActivity extends Activity {
        private static final String T = "log";
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;
        private String[] mVociMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.vociMenu);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                //R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                                R.drawable.temagold_ic_navigation_drawer,
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }

                //apre il nav drawer all'avvio
                mDrawerLayout.openDrawer(Gravity.LEFT);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         // The action bar home/up action should open or close the drawer.
         // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch(item.getItemId()) {
        case R.id.action_websearch:
            // create intent to perform web search for this planet
            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
            intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
            // catch event that there's no activity to handle intent
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            } else {
                Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        selectItem(position);

                }
    }

    private void selectItem(int position) {
                //funziona con classe FT interna.............//
/*              FragmentTest ft = new FragmentTest();
        Bundle args = new Bundle();
                args.putInt(FragmentTest.ARG_PLANET_NUMBER, position);
                ft.setArguments(args);
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, ft).commit();
                mDrawerList.setItemChecked(position, true);
                setTitle(mPlanetTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);
*/

                if(position%2 == 0) {
                        //  classe FC esterna
                        FragmentConversioni fConv = new FragmentConversioni();
                        Bundle args = new Bundle();
                        args.putInt(FragmentConversioni.ARG_PLANET_NUMBER, position);
                        fConv.setArguments(args);
                        FragmentManager fragmentManager = getFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.content_frame, fConv).commit();
                        mDrawerList.setItemChecked(position, true);
                        setTitle(mPlanetTitles[position]);
                        mDrawerLayout.closeDrawer(mDrawerList);
                }else {
                        // funziona classe FT esterna
                        FragmentTest ft = new FragmentTest();
                        Bundle args = new Bundle();
                        args.putInt(FragmentTest.ARG_PLANET_NUMBER, position);
                        ft.setArguments(args);
                        FragmentManager fragmentManager = getFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.content_frame, ft).commit();
                        mDrawerList.setItemChecked(position, true);
                        setTitle(mPlanetTitles[position]);
                        mDrawerLayout.closeDrawer(mDrawerList);
                }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    /**
     * Fragment that appears in the "content_frame", shows a planet
     */


/*      public static class FragmentTest extends Fragment {
                public static final String ARG_PLANET_NUMBER = "planet_number";

                public FragmentTest() {
                        // Empty constructor required for fragment subclasses
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                                 Bundle savedInstanceState) {
                        View vista = inflater.inflate(R.layout.fragment_conversioni, container, false);
//                      int i = getArguments().getInt(ARG_PLANET_NUMBER);
//                      String planet = getResources().getStringArray(R.array.planets_array)[i];
//
//                      int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
//                                      "drawable", getActivity().getPackageName());
//                      ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
//                      getActivity().setTitle(planet);

                        return vista;
                }
        }*/

}
« Ultima modifica: 12 Novembre 2014, 18:20:43 CET da lucifer »