我有一个带有微调器布局的菜单项,它正确显示在我的操作栏菜单中。但是,当我单击微调器时,什么也没有发生。我在 onOptionsItemSelected 中有日志消息,但在 logcat 中没有看到任何消息。以下是我的代码片段:
菜单布局
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_item_1"
android:orderInCategory="100"
android:showAsAction="always"
android:actionLayout="@layout/spinner_layout" />
</menu>
微调器布局
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/spinner_drawable">
</Spinner>
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
spinner = (Spinner) menu.findItem(R.id.menu_item_1).getActionView();
MySpinnerAdapter spinnerAdapter = new MySpinnerAdapter(this);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(spinnerAdapter);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
log.error("ON OPTIONS ITEM SELECTED FOR ITEM: " + item.getItemId());
switch (item.getItemId())
{
case R.id.menu_item_1:
log.error("ON OPTIONS ITEM SELECTED - SPINNER MENU ITEM SELECTED");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
MySpinnerAdapter.java
@Singleton 公共类 MySpinnerAdapter 扩展 ArrayAdapter 实现 SpinnerAdapter,OnItemSelectedListener{
@Inject
public TopbarErrorSpinnerAdapter(Context context) {
super(context, R.layout.my_spinner_item);
this.values = Collections.synchronizedList(new ArrayList<MySpinnerRowEntry>());
this.context = context;
this.setDropDownViewResource(R.layout.my_spinner_item);
}
@Override
public int getCount() {
return values.size();
}
@Override
public MySpinnerRowEntry getItem(int position) {
return values.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = View.inflate(context, R.layout.my_spinner_item, null);
ImageView image = (ImageView) row.findViewById(R.id.my_spinner_item_icon);
return image;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
final int rowPosition = position;
View row = View.inflate(context, R.layout.my_spinner_item, null);
TextView title = (TextView) row.findViewById(R.id.my_spinner_item_title);
title.setText(values.get(position).getTitle());
TextView message = (TextView) row.findViewById(R.id.my_spinner_item_message);
message.setText(values.get(position).getMessage());
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mySpinnerAdapterlistener.mySpinnerItemSelected(values.get(rowPosition).getType());
}
});
return row;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
MainActivity activity = (MainActivity) context;
(activity).onOptionsItemSelected((MenuItem) activity.findViewById(R.id.menu_item_1));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
如果选择的菜单项是微调器,我也不确定如何触发微调器。我在以前的不包含 ActionBar 的活动中使用了相同的微调器。由于升级到 SDK 14 后我的应用程序发生了变化,我知道必须将微调器添加到 ActionBar 的菜单中。
提前感谢大家的帮助。