Can't display sub-menu for custom ActionProvider

断了今生、忘了曾经 提交于 2019-12-21 06:34:10

问题


I've created a custom ActionProvider that I want to use to show a sub-menu for sorting, similar visually to ShareActionProvider. The action view displays as expected, but clicking on the icon doesn't display the sub-menu or show any visual feedback (pressed state) at all. I'm using the support v7 library for backward compatibility action bar. Is there an implementation I am missing to display the menu?

ActionProvider:

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:

<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>

回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/19439106/cant-display-sub-menu-for-custom-actionprovider

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