菜单项actionViewClass Spinner Spinner setOnItemSelectedListener无法正常工作

时间:2018-04-20 01:32:33

标签: android

我的菜单中有一个项目actionViewClass Spinner

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/spinner"
    android:background="@color/colorPrimary"
    android:title="Choose Sheets"
    app:actionViewClass="android.widget.Spinner"
    app:showAsAction="ifRoom" />

<item
    android:id="@+id/refresh"
    android:icon="@drawable/refresh"
    android:title="Refresh"
    app:showAsAction="ifRoom" />

</menu>

这是我的OnCreateOptionMenu

     @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.home, menu);
    item = menu.findItem(R.id.spinner);
    spinner = (Spinner) item.getActionView();
    spinnerAdapter = new MySpinnerAdapter(headers, this);
    spinner.setAdapter(spinnerAdapter);
    spinner.setOnItemSelectedListener(this);
    return true;
}

这是我的已实施方法

     @Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    Log.e("YAHOO", i+ " Position");
    Toast.makeText(this, i+ " Position" , Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

当我点击我的微调器内的项目时,没有任何反应。 actionViewClass Spinner上有一些错误吗?

1 个答案:

答案 0 :(得分:0)

关于如何在操作栏中实现微调器,这是一个很好的sample code

    actionBar = getSupportActionBar();
    // Set below attributes to add logo in ActionBar.
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayUseLogoEnabled(true);

    ...


    ...

    // Set action bar navigation mode to list mode.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    // Set action bar list navigation data and item click listener.
    actionBar.setListNavigationCallbacks(spinnerAdapter, new ActionBar.OnNavigationListener() {
        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {

            String menuItemText = dropDownItemArr[itemPosition];
            String message = "You click menu " + menuItemText;
            switch (itemPosition) {
                case 0:
                    // Click News menu item.
                    Toast.makeText(ActionBarSpinnerActivity.this, message, Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        }
    });

希望这有帮助