上下文操作菜单在视觉上不超过片段中操作栏的位置 [英] Contextual Action Menu not visually overtaking the Action Bar position in a Fragment
问题描述
我有一个 Activity
和一个 Fragment
,其中包含带有项目的 ListView
,用户可以在其上单击并调用上下文操作模式.
我想发生的事情是后,我找到了解决方案 >
I have an Activity
with a Fragment
containing a ListView
with items, on which the user can click and invoke the Contextual Action Mode.
What I like to happen is as the documentation is stating:
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.
However, this is the behavior I'm currently experiencing. As of now, the Contextual Action Mode appears above the ActionBar
, as the figure below shows.
What I've tried so far without success:
- Moving the
ActionMode
logic from theFragment
to the hostActivity
. - Setting
<item name="windowActionModeOverlay">true</item>
in my theme. - Call
getActivity().getMenuInflater()
instead ofmode.getMenuInflater()
.
This is my code where I invoke the Contextual Action Menu
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;
}
};
}
EDIT:
This is the Activity
, in which the Fragment
resides:
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
}
}
Why is the Contextual Action Menu not visually overtaking the ActionBar
, as the documentation state is should?
Solution to this was to add
<item name="android:windowActionModeOverlay">true</item>
to my Theme
, which now looks like
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionModeOverlay">true</item>
</style>
I found the solution after looking at this answer
这篇关于上下文操作菜单在视觉上不超过片段中操作栏的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!