Can't display sub-menu for custom ActionProvider

有些话、适合烂在心里 提交于 2019-12-03 21:38:24

Unfortunately, onPrepareSubMenu() is only called when onCreateActionView() returns null. Your solution (attaching a PopupMenu to the ImageView) will work, but since your ImageView is displaying an icon, you might consider just setting the icon in the menu XML and getting rid of the ImageView:

<item android:id="@+id/menu_sort"
      android:icon="@drawable/ic_action_sort_by_size"
      android:title="@string/sort"
      support:actionProviderClass="com.myapp.provider.SortActionProvider"
      support:showAsAction="always" />

and then:

@Override
public View onCreateActionView(){
    return null;
}

This should also solve the problem with no visual feedback for touch states - the problem being that your ImageView is not configured to do anything with touch states.

The ActionProvider doesn't show the sub-menu when you click on the action view. I'm actually not sure under what conditions the sub-menu is displayed. What I had to do was attach a PopupMenu to the action view:

@Override
public View onCreateActionView(){
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(R.drawable.ic_action_sort_by_size);

    final PopupMenu menu = new PopupMenu(mContext, imageView);
    menu.inflate(R.menu.sort_options_menu);
    menu.setOnMenuItemClickListener(this);

    imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v){
            menu.show();
        }
    });

    return imageView;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!