android - 使用支持库时如何在 ListView 中提供 ContextMenu?

标签 android android-listview actionbarsherlock android-support-library

我在使用 ActionBarSherlock 库时尝试膨胀 ContextMenu 时遇到类型问题,如您所知,该库最终模仿/实现了 Android 支持库。

有一个SherlockFragmentActivity,它设置布局内容,并且该内容中存在两个 fragment 。这些 fragment 之一是 SherlockListFragment。在 SherlockListFragmentonCreate 中,我调用注册 ContextMenu

registerForContextMenu(getListView());

问题源于尝试膨胀菜单。

listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
       @Override
       public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
                MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
                inflater.inflate(R.menu.lot_menu, contextMenu);
            }
        });

由于方法参数中指定的类型,我无法调用 inflate,它预计为 com.actionbarsherlock.view.Menu 但传入的类型是android.view.ContextMenu.

我似乎遗漏了一些东西,因为在操作栏中膨胀Menu是微不足道的,但是ContextMenu在使用支持框架时似乎会带来问题。

如何正确注册以根据需要使用支持框架并随后扩展 ContextMenu

最佳答案

尝试使用 getMenuInflater() 而不是 getSupportMenuInflater() 来膨胀为 ContextMenu

关于android - 使用支持库时如何在 ListView 中提供 ContextMenu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468209/

相关文章:

android - 有没有办法禁用除可扩展 ListView Android中单击的父 View 之外的所有父 View ?

java - 禁用 TextView 中的焦点

android - picasso 图书馆 : ImageView in ListView Shows loads image multiple time on fast Srcroll

android - MultiChoiceModeListener 导致 SherlockListFragment 出现问题

android - ActionbarSherlock 列表导航 - 滚动到顶部/聚焦第一行

android - 在Android Media Player中启用DVR功能

java - 更改 ViewPager 内显示的 fragment

Android 工作管理器一次性请求从工作人员返回空白结果

android - ActionBarSherlock 和 Action Bar.On 导航监听器

java - Android Canvas drawLine 未在 MainActivity 上绘制