简体   繁体   English

是否可以在Android的操作栏中使用滑动标签和微调器

[英]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. 我目前有一个带有滑动选项卡的活动,并且我一直在使用ActionBarSherlock,但我想在操作栏中添加一个微调器,以便用户可以从微调器中选择项目,并且每个项目都是一个特定的文件,可以打开值重新填充每个选项卡上的视图。 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? 我看过类似的问题, 是否可以在操作栏中使用下拉AND标签作为导航? 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. 我也刚刚看过我拥有的一个Android应用程序,它是Eurosport应用程序,并且如果有人拥有该应用程序,可以查看我的意思。

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: 按下Spinner时,我在logcat中收到以下消息:

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: 因此,首先创建一个标记文件来放置Spinner ,例如:

<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: 接下来创建一个菜单资源,例如main_menu.xml并将其放入res/menu文件夹:

<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 . 请注意,我们为此菜单项指定了自定义操作布局-这是我们之前创建的布局,其中包含Spinner Next in your Activity (or Fragment ) you must inflate this menu: 接下来,您必须在Activity (或Fragment )中添加以下菜单:
Use this if you creating this menu in Activity 如果您在“ 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 : 或者,如果您在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() . 如果您为Fragment创建此菜单,请不要忘记在Fragment.onCreate()某个位置调用setHasOptionMenu(true) Fragment.onCreate()

Put this instead of ... // init spinner 放置它而不是... // 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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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