动作状态栏
按下状态::
默认状态::
我在做什么 ::
我正在使用拆分的操作栏
单击底部的图标后,我正在更改片段
功能正常
我想要的是::
是否可以保持操作栏项目的按下状态直到
我单击下一个动作栏图标
然后,当我说单击图中的星星时,我要保留星星
按下状态为蓝色,并显示搜索图标以返回正常状态
我使用的代码::
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.searchID){
//SEARCH Button Handling
//((View) item).setBackgroundResource(R.color.actionBarIconPressed);
//item.setIcon(R.color.actionBarIconPressed);
item.setEnabled(true);
ft1=getSupportFragmentManager().beginTransaction();
ft1.hide(fragment1);
//Condition to check whether the fragment is already in container & based on that do appropriate actions
if (getSupportFragmentManager().findFragmentByTag("SearchFragmentTag")==null){
ft1.add(R.id.content_frame, fragSearch, "SearchFragmentTag");
ft1.addToBackStack(null);
ft1.commit();
}else{
ft1.remove(fragSearch);
ft1.add(R.id.content_frame, fragSearch, "SearchFragmentTag");
ft1.commit();
}
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.actionbar_sort_menu, menu);
return super.onCreateOptionsMenu(menu);
}
我的问题:
如何修改我的代码以实现此功能?
可能吗 ?
有哪些可能的方法?
最佳答案
在onOptionsItemSelected
中,可以将MenuItem
转换为View
,然后更改其背景。我有一个初步的答案,但是我认为您可以根据自己的需要进行改进和更改:
将您的ID初始化为数组:
int[] listItemId = { R.id.searchID, R.id.ratindID, R.id.likeID, R.id.shareID };
调用您的选项项选择方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.searchID:
// call private method with int 1
onItemChangeBackground(1);
... // do some stuff
return true;
case R.id.ratindID:
onItemChangeBackground(2);
... // do some stuff
return true;
case R.id.likeID:
onItemChangeBackground(3);
...
return true;
case R.id.shareID:
onItemChangeBackground(4);
...
return true;
}
return super.onOptionsItemSelected(item);
}
通过切换int值来更改背景项:
private void onItemChangeBackground(int i) {
// Loop to reinit the background items
for(int ii = 0; ii < listItemId.length; ii++) {
// Cast the MenuItem into a View
( (View) findViewById(listItemId[ii]) )
// And set the default background (for example dark)
.setBackgroundColor(getResources().getColor(R.color.dark));
}
// Selected background
switch(i) {
case 1: ( (View) findViewById(R.id.searchID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 2: ( (View) findViewById(R.id.ratindID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 3: ( (View) findViewById(R.id.likeID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 4: ( (View) findViewById(R.id.shareID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
}
}
当您要重新初始化背景项目时(也可以像在styles.xml中一样),还可以使用
setBackgroundDrawable()
使用选择器设置可绘制对象。经过测试,有效!另一个解决方案可能是使用
invalidateOptionsMenu()
。再次调用onCreateOptionsMenu()
将清除并重新绘制菜单。请记住,在没有调用invalidateOptionsMenu()
的情况下,永远不会调用最后一个方法,它仅在活动创建的第一个边缘被调用。这就是为什么我们需要使它的内容无效:为所选的每个项目更新一个整数:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.searchID){
nbItemSelected = 1;
invalidateOptionsMenu();
...
} else if(...) {
nbItemSelected = 2;
invalidateOptionsMenu();
} ...
}
然后,在创建选项时更改背景:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
if(nItemSelected != 0) {
// Find item, cast the selected item to a view
( (View) menu.findItem(listItemId[nItemSelected]) )
// Change its background
.setBackgroundColor(getResources().getColor(R.color.blue));
}
...
return true;
}
我想也可以用这种方式,它可能会起作用。