我想在我的应用程序中使用上下文操作栏 (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;
}
...
我有以下错误:
- -"activity.startActionMode(mActionModeCallback);":Activity类型中的方法startActionMode(ActionMode.Callback)不适用于参数(ActionMode.Callback)
- -activity.getSupportMenuInflater().inflate(R.menu.cab_songs, menu);":方法 getSupportMenuInflater() 对于 FragmentActivity 类型未定义
任何想法?使用 sherlock 的 CAB 是否有另一种解决方案?