问题
I am setting the action bar and item by the below code and the respective image1 is shown. When the user clicks on show bookmark screen action item, it goes to other activity. In that activity I want another item(SELECT BOOKMARK TYPE ) to be displayed in the place of SHOW BOOKMARK SCREEN. So I am thinking to managing it with abstract class by setting the respective things to true or false as shown below. But now I am unable to get two things.
1)
How to differentiate in case 0 for both action items? as I am replacing the action item with one another.
2)
How to get the dropdown for that SELECT BOOKMARK TYPE as exactly shown in the image 2.
Have seen few posts, but as I am somewhat new to android, I am unable to understand and get it done by adding the extra code to my present code. Can you please help me on this? Code snippets are appreciated. Thanks in advance.
public abstract class ActionActivity extends SherlockActivity {
protected boolean mIsShowBookmarkScreen = true;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if(mIsShowBookmarkScreen)
{
menu.add("SHOW BOOKMARK SCREEN")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
else
{
menu.add(SELECT BOOKMARK TYPE);
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//This uses the imported MenuItem from ActionBarSherlock
switch(item.getItemId())
{
case 0:
Intent intent = new Intent(ActionActivity.this,BookmarkScreen.class);
startActivity(intent);
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setHomeButtonEnabled(true);
}
}
image 1:
image 2:
At least can someone please help on achieving second one. I got an idea on 1st problem.
回答1:
1) Differentiate by using instanceof
.
if (this instanceof ActivityA) {
// start Intent A
} else if (this instanceof ActivityB) {
// start Intent B
}
2) Add a Spinner
as a custom ActionView
.
<string-array name="items">
<item>SELECT BOOKMARK TYPE</item>
<item>TYPE-1</item>
<item>TYPE-2</item>
<item>TYPE-3</item>
</string-array>
MenuItem spinnerItem = menu.add(null);
spinnerItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
IcsSpinner spinner = new IcsSpinner(this, null);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.items, R.layout.sherlock_spinner_item);
adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
spinnerItem.setActionView(spinner);
Note that I am using the kind of private IcsSpinner
here to create the same look on SDK Version < 4.0. See this answer for details.
If you want to further customize the Spinner
, you will probably need to create your own Adapter
.
来源:https://stackoverflow.com/questions/14080269/setting-the-dropdown-for-the-action-bar-item