android - 如何将上下文操作模式与 SherlockListFragment 结合使用

标签 android android-actionbar actionbarsherlock

我想在我的应用程序中使用上下文操作栏 (CAB),但与旧版本的 Android 不兼容,因此我使用本教程:http://www.miximum.fr/tutos/849-porting-the-contextual-anction-mode-for-pre-honeycomb-android-apps 我的代码是:

public class SongsFragment extends SherlockListFragment implements
    LoaderManager.LoaderCallbacks<Cursor>, OnLongClickListener{

...
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    activity = this.getActivity();
    ...
    mMode = null;
    mListView = getListView();
    mListView.setItemsCanFocus(false);
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    mListView.setOnLongClickListener(this);

}

@Override
public boolean onLongClick(View v) {
    SparseBooleanArray checked = mListView.getCheckedItemPositions();
    boolean hasCheckedElement = false;
    for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
        hasCheckedElement = checked.valueAt(i);
    }

    if (hasCheckedElement) {
        if (mMode == null) {
            mMode = activity.startActionMode(mActionModeCallback);
        }
    } else {
        if (mMode != null) {
            mMode.finish();
        }
    }
    return false;
}

 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Create the menu from the xml file
        activity.getSupportMenuInflater().inflate(R.menu.cab_songs, menu);
        return true;
    }
    ...

我的错误是:

  1. -"activity.startActionMode(mActionModeCallback);":类型Activity中的方法startActionMode(ActionMode.Callback)不是 适用于参数 (ActionMode.Callback)
  2. -activity.getSupportMenuInflater().inflate(R.menu.cab_songs, menu);":方法 getSupportMenuInflater() 未定义 类型 FragmentActivity

有什么想法吗?使用 sherlock 的 CAB 是否还有其他解决方案?

最佳答案

ActionModeMenuInflater 的导入更改为其 ActionBarSherlock 等效项(com.actionbarsherlock.view.ActionModecom.actionbarsherlock .view.MenuInflater)。

关于android - 如何将上下文操作模式与 SherlockListFragment 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14296963/

相关文章:

java - 我训练的图像分类器模型对所有不属于该类别的图像进行分类

android - 操作栏选项卡选择分隔线颜色

android - SherlockFragment 无法转换为 Fragment,我的 Activity 是 SherlockActivty

android - WebView.draw(canvas) 有时会在某些设备上的 Android Lollipop 5.0+ 中崩溃 - 致命信号 11 (SIGSEGV)

android - 如何在 Android Wear 中动态更改 Action 图标

android - 如何使用 firebase-server 在 android 下模拟 firebase 云消息传递

android - ActionBar 选项卡宽度

android - 使用自定义 View 作为操作栏图标

Android 兼容性问题(Api 8、ActionbarSherlock)

android - 从 SherlockFragmentActivity 覆盖 onOptionsItemSelected