当我有一个在标准 ListActivity 上工作的上下文操作栏时,我有一个扩展 ListView.MultiChoiceModeListener 的类,但这不适用于 ListFragment 类或标准活动,所以我将如何实现它。
我不明白怎么MultiChoiceModeListener
不可用(也许我不明白你想做什么)。根据您的评论,我假设您使用了兼容性包中的片段。下面是一个FragmentActivity
带有两个静态片段的示例,每个片段都使用自己的菜单选项触发上下文操作栏。
FragmentActivity
非常简单,它只包含以下两个片段:
// the list fragment
public class ListCABFragment extends ListFragment {
private String[] mCountries = { "Romania", "Germany", "England", "USA",
"Japan", "France" };
private static final boolean POST_HONEYCOMB = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (POST_HONEYCOMB) {
// make sure we are on a version above Honeycomb otherwise will
// access things that aren't available
postHoneycombCAB();
} else {
// probably do nothing and implement the normal context menu?!?
}
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, mCountries));
}
@SuppressLint({ "NewApi", "NewApi" })
private void postHoneycombCAB() {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
((ListView) parent).setItemChecked(position,
((ListView) parent).isItemChecked(position));
return false;
}
});
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
getActivity().getMenuInflater().inflate(R.menu.listcab_menu,
menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getActivity(), "Option1 clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(getActivity(), "Option2 clicked",
Toast.LENGTH_SHORT).show();
break;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
nr = 0;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
if (checked) {
nr++;
} else {
nr--;
}
mode.setTitle(nr + " rows selected!");
}
});
}
}
还有A的另一个片段,该片段Fragment
的布局是由A组成的,该布局在选择ARadioGroup
时触发CAB RadioButton
:
public class SimpleCABFragment extends Fragment implements Callback {
private static final boolean POST_HONEYCOMB = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_simplecabfragment, container,
false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
RadioGroup rg = (RadioGroup) getView().findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (POST_HONEYCOMB) {
// this could be improved so we don't need to create the
// option
// menu if it is already available
getActivity().startActionMode(SimpleCABFragment.this);
} else {
// something else
}
}
});
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == R.id.itemradio) {
Toast.makeText(getActivity(), "CAB for Radiogroup!",
Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
getActivity().getMenuInflater().inflate(R.menu.simplecab_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
}
看看这是否是您正在寻找的(您可以在我的 github 项目中找到包括布局和菜单文件的完整示例)。