工具栏在操作模式下不会消失 [英] Toolbar won't disappear in action mode
问题描述
我正在尝试实现以下功能:长时间单击列表项时,将启动操作模式,并且可以删除一个或多个项目.
我从DocumentsActivity
开始搜索,该搜索使用ListView及其项开始一个片段DocumentsFragment
. ListAdapter通过Fragment的onCreate中的方法调用setListAdapter(this.documentsAdapter)
进行初始化和设置.我在片段的onActivityCreated
中的列表视图上设置了各种侦听器:
I am trying to implement the feature that, when I am long clicking a list item, the action mode shall start and it shall be possible to delete one or more items.
I am starting in DocumentsActivity
a search, which starts a Fragment DocumentsFragment
with a ListView and their items. The ListAdapter is initialized and set via method call setListAdapter(this.documentsAdapter)
in onCreate of Fragment. I set various listeners on the listview in the onActivityCreated
in the Fragment:
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
getListView().setItemChecked(position, true);
return true;
}});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
((DocumentsActivity)getActivity()).getMenuInflater().inflate(R.menu.documents_context_menu, menu);
return true;
}
});
super.onActivityCreated(savedInstanceState);
}
长按列表项时,动作模式开始,并且菜单documents_context_menu
似乎是动作栏.但是问题是,操作栏出现在工具栏上方,并且工具栏不会消失(参见图片).
When long clicking on a listitem the action mode gets started and the menu documents_context_menu
appears to be the action bar. But the problem is, the action bar appears above the toolbar and the toolbar won't disappear (see the picture).
我尝试调用getSupportActionBar().hide()
或将其设置为null甚至使用其他样式/主题.一切都没有用.有时蓝色的工具栏是完全白色的,但是仅此而已.
I've tried to call getSupportActionBar().hide()
or set it to null or even use another style/theme. It all didn't work. Sometimes the blue toolbar was completely white, but that is all.
我绝对不知道为什么工具栏不会消失.你可以给点建议吗?
I have absolutely no idea why the toolbar won't disappear. May you give some advice?
提前谢谢!
_____更新1 _____
这是styles.xml
This is the styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:fitsSystemWindows">true</item>
<item name="colorAccent">@color/darkblue100</item>
<item name="android:actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="android:actionMenuTextColor">@color/black</item>
</style>
这是在活动"中设置操作栏的方式:
And this is how the action bar is set in the Activity:
protected void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
requestWindowFeature(5);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents);
Toolbar mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
args = getIntent().getExtras();
if (findViewById(R.id.container_documents) != null && savedInstanceState == null) {
showDocumentsFragment();
}
}
推荐答案
实际上,此问题是由不同的原因引起的.
首先,将windowActionBar
设置为true
,也是属性fitsSystemWindows.
我删除了styles.xml
中的两行.
Actually the issue were caused by different things.
First, the windowActionBar
was set true
, also was the attribute fitsSystemWindows.
I deleted both lines in styles.xml
.
然后,在活动布局中,我完全看不到属性layout_marginTop="?actionBarSize"
,我没有看到.但是该属性必须存在,因此在调用onCreateActionMode
和onDestroyActionMode
时,我可以在方法onActivityCreated
中对其进行处理.
Then there was in the activity layout the attribute layout_marginTop="?actionBarSize"
I did not see, following in complete confusion. But this attribute needs to be there so I am handling it in the method onActivityCreated
when onCreateActionMode
and onDestroyActionMode
are called.
那之后,我就自动地遇到了列表视图项消失的问题.我通过在onDestroyActionMode
中再次提交片段修复了该问题.
After that all I had the automagically problem that the listview items disappeared. I fixed it by commiting my fragment again in onDestroyActionMode
.
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {...}
});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
LinearLayout ll = ((DocumentsActivity)getActivity()).findViewById(R.id.container_documents);
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, 0, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().hide();
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.documents_context_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, R.attr.actionBarSize, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().show();
if (getFragmentManager() != null)
getFragmentManager()
.beginTransaction()
.detach(this)
.attach(this)
.commit();
}
});
super.onActivityCreated(savedInstanceState);
}
这篇关于工具栏在操作模式下不会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!