简体   繁体   中英

Is it possible to use sliding tabs and a spinner in the action bar in Android

I currently have an Activity with sliding tabs on it and I have been using ActionBarSherlock, but I wanted to have a spinner in the action bar, so that the user could select items from the spinner and each item is a specific file which opens up values to re-populate the views on each tab. I have looked to see how you can do this and so far have not had any luck. I don't want them to both navigate through fragments, but instead have the spinner to re-populate views on the tabs. I looked at a similar question Is it possible to use dropdown AND tabs as navigation in the action bar? which is how I want it to look, but his question was to use two different types of navigation. Please can anyone help me and let me know if this is possible. Thank you in advance. I have also just looked at an Android application that I have which is the Eurosport app and that has the type of layout that I am looking for if anyone has that application to see what I mean.

Here is my code:

public class SlidingTabsActivity extends SherlockFragmentActivity
{

ViewPager viewPager;
TabsAdapter tabsAdapter;
ActionBar actionBarTabs;
Spinner spinner;
ArrayAdapter<String> arrayAdapter;
LayoutInflater spinnerLayoutInflater;
View spinnerView;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    viewPager = new ViewPager(this);
    viewPager.setId(R.id.pager);
    setContentView(viewPager);

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.device_description);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerLayoutInflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    spinnerView = spinnerLayoutInflater.inflate(R.layout.spinner, null);
    spinner = (Spinner) spinnerView.findViewById(R.id.tabsSpinner);
    spinner.setAdapter(arrayAdapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener()
    {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) 
        {
            // TODO Auto-generated method stub

        }

    });

    actionBarTabs = getSupportActionBar();
    actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBarTabs.setCustomView(spinnerView);
    actionBarTabs.setDisplayShowCustomEnabled(true);
    actionBarTabs.setDisplayHomeAsUpEnabled(true);

    tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view

    /* Adds fragments to the tabs adapter */
    tabsAdapter.addTab(actionBarTabs.newTab().setText("PV"), Fragment_1.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("CONFIG"), Fragment_2.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("DIAG"), Fragment_3.class, null);

}

Here is my Spinner code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<Spinner
    android:id="@+id/tabsSpinner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

I get this message in the logcat when pressing the Spinner:

11-26 12:22:44.282: W/InputEventReceiver(7217): Attempted to finish an input event but the input event receiver has already been disposed.
11-26 12:22:44.282: W/InputMethodManagerService(525): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4216a290 attribute=null, token = android.os.BinderProxy@42618330

So, first of all create a markup file to place your Spinner , eg:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:spinnerMode="dropdown" >
    </Spinner>

</RelativeLayout>  

Next create a menu resource, eg main_menu.xml and put it to res/menu folder:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_change_content"
        android:actionLayout="@layout/spinner_layout"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/empty">
    </item>

</menu>  

Paid your attention that we specified custom action layout for this menu item - this is a layout we have created earlier and it contains our Spinner . Next in your Activity (or Fragment ) you must inflate this menu:
Use this if you creating this menu in Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    ...// init spinner, will be explained below
    return true;
}

Or if you creating menu in Fragment :

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.card_storage, menu);
    ...// init spinner
}  

If you creating this menu for Fragment don't forget to call setHasOptionMenu(true) somewhere in Fragment.onCreate() .

Put this instead of ... // init spinner

MenuItem item = menu.findItem(R.id.action_change_content); // id of our custom menu item
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(); // your adapter
Spinner spinner = (Spinner) item.getActionView().findViewById(R.id.spinner); 
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);  

I hope I didn't missed out something. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM