上下文操作菜单不会在视觉上超过片段中的操作栏位置
Contextual Action Menu not visually overtaking the Action Bar position in a Fragment
我有一个 Activity
和一个 Fragment
包含一个 ListView
的项目,用户可以点击并调用上下文操作模式。
我希望发生的事情正如 documentation 所说:
The contextual action bar is not necessarily associated with the
action bar. They operate independently, even though the contextual
action bar visually overtakes the action bar position.
但是,这是我目前遇到的行为。截至目前,上下文操作模式出现在上方ActionBar
,如下图所示。
到目前为止我尝试过但没有成功的方法:
- 将
ActionMode
逻辑从 Fragment
移动到主机 Activity
。
- 在我的主题中设置
<item name="windowActionModeOverlay">true</item>
。
- 调用
getActivity().getMenuInflater()
而不是 mode.getMenuInflater()
。
这是我调用上下文操作菜单的代码
public class NotesFragment extends Fragment implements View.OnClickListener{
private ActionMode mActionMode;
@Override
public void checkBoxChecked(Note which) {
if (mActionMode == null)
mActionMode = getActivity().startActionMode(mActionModeCallback);
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
// Called each time the action mode is shown.
// Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.context_delete:
//Do work unrelated to topic
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.context_move:
//Do work unrelated to topic
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
编辑:
这是 Activity
,其中 Fragment
所在:
public class MainActivity extends ActionBarActivity implements DialogFragmentMoveNote.DialogFragmentMoveNoteListener,
DialogFragmentRemoveNote.DialogFragmentRemoveNoteListener, DialogFragmentAddNewFolder.DialogFragmentAddNewFolderListener,
DialogFragmentDeleteFolder.DialogFragmentDeleteFolderListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onAddNewFolderPositiveClick(Folder folder) {
//Tell the fragment to do work
}
@Override
public void onRemoveNotesPositiveClick() {
//Tell the fragment to do work
}
@Override
public void onMoveNotePositiveClick(String chosenFolder) {
//Tell the fragment to do work
}
@Override
public void onDeleteFolderPositiveClick() {
//Tell the fragment to do work
}
private void displayNoteDetailsFromWidget(String noteId){
//Tell the fragment to do work
}
}
为什么上下文操作菜单没有像文档状态那样在视觉上超过 ActionBar
?
解决这个问题的方法是添加
<item name="android:windowActionModeOverlay">true</item>
我的Theme
,现在看起来像
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionModeOverlay">true</item>
</style>
看了this answer
后找到了解决方法
我有一个 Activity
和一个 Fragment
包含一个 ListView
的项目,用户可以点击并调用上下文操作模式。
我希望发生的事情正如 documentation 所说:
The contextual action bar is not necessarily associated with the action bar. They operate independently, even though the contextual action bar visually overtakes the action bar position.
但是,这是我目前遇到的行为。截至目前,上下文操作模式出现在上方ActionBar
,如下图所示。
到目前为止我尝试过但没有成功的方法:
- 将
ActionMode
逻辑从Fragment
移动到主机Activity
。 - 在我的主题中设置
<item name="windowActionModeOverlay">true</item>
。 - 调用
getActivity().getMenuInflater()
而不是mode.getMenuInflater()
。
这是我调用上下文操作菜单的代码
public class NotesFragment extends Fragment implements View.OnClickListener{
private ActionMode mActionMode;
@Override
public void checkBoxChecked(Note which) {
if (mActionMode == null)
mActionMode = getActivity().startActionMode(mActionModeCallback);
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
// Called each time the action mode is shown.
// Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.context_delete:
//Do work unrelated to topic
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.context_move:
//Do work unrelated to topic
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
编辑:
这是 Activity
,其中 Fragment
所在:
public class MainActivity extends ActionBarActivity implements DialogFragmentMoveNote.DialogFragmentMoveNoteListener,
DialogFragmentRemoveNote.DialogFragmentRemoveNoteListener, DialogFragmentAddNewFolder.DialogFragmentAddNewFolderListener,
DialogFragmentDeleteFolder.DialogFragmentDeleteFolderListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onAddNewFolderPositiveClick(Folder folder) {
//Tell the fragment to do work
}
@Override
public void onRemoveNotesPositiveClick() {
//Tell the fragment to do work
}
@Override
public void onMoveNotePositiveClick(String chosenFolder) {
//Tell the fragment to do work
}
@Override
public void onDeleteFolderPositiveClick() {
//Tell the fragment to do work
}
private void displayNoteDetailsFromWidget(String noteId){
//Tell the fragment to do work
}
}
为什么上下文操作菜单没有像文档状态那样在视觉上超过 ActionBar
?
解决这个问题的方法是添加
<item name="android:windowActionModeOverlay">true</item>
我的Theme
,现在看起来像
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionModeOverlay">true</item>
</style>
看了this answer
后找到了解决方法