我创建了一个自定义ActionProvider
,我想用它来显示一个用于排序的子菜单,在视觉上类似于ShareActionProvider
. 操作视图按预期显示,但单击图标不会显示子菜单或显示任何视觉反馈(按下状态)。我正在使用 support v7 库来实现向后兼容操作栏。我是否缺少显示菜单的实现?
动作提供者:
public class SortActionProvider extends ActionProvider implements OnMenuItemClickListener {
private Context mContext;
public SortActionProvider(Context context) {
super(context);
mContext = context;
}
@Override
public View onCreateActionView(){
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(R.drawable.ic_action_sort_by_size);
return imageView;
}
@Override
public boolean hasSubMenu(){
return true;
}
@Override
public void onPrepareSubMenu(SubMenu subMenu){
subMenu.clear();
subMenu.add("Sort by name").setOnMenuItemClickListener(this);
subMenu.add("Sort by type").setOnMenuItemClickListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem item){
Toast.makeText(mContext, "I was clicked!", Toast.LENGTH_SHORT).show();
return true;
}
}
菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:support="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_sort"
android:actionProviderClass="com.myapp.provider.SortActionProvider"
android:showAsAction="always"
android:title="@string/sort"
support:actionProviderClass="com.myapp.provider.SortActionProvider"
support:showAsAction="always"/>
</menu>