将上下文操作栏与片段一起使用 [英] Using Contextual action bar with fragments
问题描述
我目前正在开发 android 项目并使用片段和 ListViews/ListFragments.我有处理标准活动(例如 ListActivity)的上下文操作栏.
I am currently working on android project and making use of fragments and ListViews/ListFragments. I have contextual action bars working on standard activities such as a ListActivity.
现在我正在尝试做同样的事情,但在片段布局上.我有一个扩展 Activity 的 MainActivity,它为包含 2 个片段(片段 A 和片段 B)的布局扩充 XML.
Now I am trying to do the same sort of thing but on a fragment layout. I have a MainActivity which extends Activity which inflates the XML for the layout that contains the 2 fragments, fragment A and fragment B.
Fragment A 扩展了 ListFragment 并包含一个 ListView,它由 SQLite 数据库中的数据填充.当我有一个处理标准 ListActivity 的上下文操作栏时,我有一个扩展 ListView.MultiChoiceModeListener 的类,但这不适用于 ListFragment 类或标准活动,所以我将如何实现它.
Fragment A extends ListFragment and contains a ListView which is populated from data within an SQLite Database. When I have got a contextual action bar working on a standard ListActivity I have a class that Extends ListView.MultiChoiceModeListener but this isn't available for a ListFragment class or a standard activity so how would I go about implementing this.
我想要实现的基本事情是,当有人长按 FragmentA 中的 ListView 中的项目时,它扩展了 ListFragment,操作栏会根据上下文发生变化,然后用户可以从 ListView 中选择多个项目.
The basic thing I want to achieve is when someone long presses the item within a ListView within FragmentA which extends ListFragment, the action bar contextually changes and the user can then select multiple items from within the ListView.
感谢您提供的任何帮助.
Thanks for any help you can provide.
推荐答案
当我有一个处理标准的上下文操作栏时ListActivity 我有一个扩展类ListView.MultiChoiceModeListener 但这不适用于ListFragment 类或标准活动,那么我该怎么做实现这一点.
When I have got a contextual action bar working on a standard ListActivity I have a class that Extends ListView.MultiChoiceModeListener but this isn't available for a ListFragment class or a standard activity so how would I go about implementing this.
我不明白 MultiChoiceModeListener
是如何不可用的(也许我不明白你想做什么).根据您的评论,我假设您使用了兼容包中的片段.下面是一个带有两个静态片段的 FragmentActivity
示例,每个片段都会触发上下文操作栏,并带有自己的菜单选项.
I don't see how MultiChoiceModeListener
isn't available (maybe I didn't understand what you try to do). From your comment I assumed you use the fragments from the compatibility package.
Below is an example with a FragmentActivity
with two static fragments and each of those fragments triggers the contextual action bar with their own menus options.
FragmentActivity
很简单,它只包含下面的两个片段:
The FragmentActivity
is very simple, it just holds the two fragments below:
// 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!");
}
});
}
}
and the other fragment for a Fragment
which has a layout composed from a RadioGroup
which triggers the CAB when a RadioButton
is selected:
and the other fragment for a Fragment
which has a layout composed from a RadioGroup
which triggers the CAB when a RadioButton
is selected:
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 项目中).
See if this is what you're looking for (you can find a full sample including the layouts and menus files in my github project).
这篇关于将上下文操作栏与片段一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!