Adding spinner to ActionBar (not Navigation

前端 未结 4 1342
灰色年华
灰色年华 2020-12-01 05:41

I have added a spinner to my ActionBar using the second option from the answer here .

How to I add a spinner adapter to the spinner? I tried using a Spinner object

相关标签:
4条回答
  • 2020-12-01 06:21
    inflater.inflate(R.menu.my_menu, menu); // inflate the menu 
    
    Spinner s = (Spinner) menu.findItem(R.id.my_menu_spinner).getActionView();     // find the spinner 
    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar() .getThemedContext(), R.array.my_menu_spinner_list, android.R.layout.simple_spinner_dropdown_item);    // create the adapter from a StringArray 
    s.setAdapter(mSpinnerAdapter);   // set the adapter 
    s.setOnItemSelectedListener(myChangeListener);    // (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection
    
    0 讨论(0)
  • 2020-12-01 06:25

    I know this is an old question, but just in case someone stumbles on it (as I did) and still looks for a complete answer, here is how to do it using the compatibility library, so that it works from to v7 (Android 2.1 Eclair) to current v19 (Android 4.4 KitKat):

    In menu_layout.xml:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    
      <item android:id="@+id/spinner"
        yourapp:showAsAction="ifRoom"
        yourapp:actionViewClass="android.widget.Spinner" />
    </menu>
    

    Using http://schemas.android.com/apk/res-auto namespace aliased as yourapp enables you to use the attributes showAsAction and actionViewClass that do not exist on older versions of Android.

    Then in your Activity code:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_layout, menu);
        MenuItem item = menu.findItem(R.id.spinner);
        Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
        spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
        spinner.setOnItemSelectedListener(onItemSelectedListener); // set the listener, to perform actions based on item selection
    

    Et voilà!

    0 讨论(0)
  • 2020-12-01 06:26

    I know you ditched the spinner, but I'll give some hints here in case other people is having the same problem or you come to develop the same pattern in a different app

    • If you got null is because you didn't properly specify the IDs. Double check the IDs.
    • on of the links you showed over complicated stuff by specifying a actionLayout that is just a spinner, you can just specify an actionViewClass="android.widget.Spinner" that will do the trick.
    • then in the OnCreateOptionsMenu you do:

      inflater.inflate(R.menu.my_menu, menu); // inflate the menu
      Spinner s = (Spinner) menu.findItem(R.id.my_menu_spinner).getActionView(); // find the spinner
      SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar()
              .getThemedContext(), R.array.my_menu_spinner_list, android.R.layout.simple_spinner_dropdown_item); //  create the adapter from a StringArray
      s.setAdapter(mSpinnerAdapter); // set the adapter
      s.setOnItemSelectedListener(myChangeListener); // (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection
      

    happy coding...

    0 讨论(0)
  • 2020-12-01 06:28

    Well, I ditched the Spinner idea for using a submenu. I realized that the spinner was for selecting things that stayed selected; submenus seamed to be a better UI fit.

    0 讨论(0)
提交回复
热议问题