我已经为菜单项创建了自定义布局。现在,当单击时,我需要它加载一个新的活动。代码逻辑如下:
资源中的菜单声明:
这个活动由各自选项卡下的碎片组成。根据我在SO上收集到的信息,我需要在片段的
现在,在活动中,两个主要的重要方法分别是
问题如下:在操作栏中点击菜单项时,它的工作方式相当“随机”。有时候,在点击一次后它会工作,而其他时候,需要点击4次或3次才能加载活动。没有一次点击的一致性。似乎存在什么问题?
资源中的菜单声明:
<item android:id="@+id/shoppingCart"
android:title="cart"
android:background="@layout/basket_notification_counter"
android:icon="@drawable/ic_add_shopping_cart_white_24dp"
app:showAsAction="always" />
这个活动由各自选项卡下的碎片组成。根据我在SO上收集到的信息,我需要在片段的
onCreateView
方法中调用setHasOptionsMenu(true);
,我已经这样做了。现在,在活动中,两个主要的重要方法分别是
onCreateOptionsMenu
和onOptionsItemSelected
,如下所示:package project.activities;
//... Imports come here
public class SalesActivity extends ActionBarActivity
{
private final static String TAG = "PROJECT";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sales);
// Setup the action bar
runOnUiThread(new Runnable() {
@Override
public void run() {
showActionBar();
}
});
}
/**
* Creates menus found the action bar
*
* @param menu the menu to work on
* @return true
*/
@Override
public boolean onCreateOptionsMenu(final Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_sale, menu);
final MenuItem item = menu.findItem(R.id.shopping_cart);
// THIS IS THE PROBLEM
// This workd but erratcally. After a number of clicks, it loads the activity specified in onOptionsItemSelected
// This is random: sometimes one click, sometimes 2 or up to 7 clicks so far.
item.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menu.performIdentifierAction(item.getItemId(), 0);
}
});
/**
// Also tried this but didn't work. Didn't also throw an exception to tell something was wrong
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
LoggedInActivity.this.showCart();
return true;
}
});
*/
return true;
}
/**
* Handles menus in lists
*
* @param item the selected item
* @return the selected item
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
switch (id) {
case R.id.shopping_cart:
Intent intent = new Intent(LoggedInActivity.this, CheckOutActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* When the screen is rotated, this method is called
*
* @param newConfig the new app configuration
*/
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
/**
* Recreates an item in cases where the app is pushed to the background
*
* @param savedInstanceState the bundle
*/
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
}
/**
* Handles the action bar
*/
public void showActionBar()
{
// Initialize the action bar
ActionBar actionBar = getSupportActionBar();
//actionBar.setElevation(0);
// Set up tabs
showActionBarTabs(actionBar);
}
/**
* Setup the actionbar tabs
* @param actionBar the actionBar we get from the activity and style
*/
public void showActionBarTabs(final ActionBar actionBar)
{
//... I set up the actionbar tabs here
}
}
问题如下:在操作栏中点击菜单项时,它的工作方式相当“随机”。有时候,在点击一次后它会工作,而其他时候,需要点击4次或3次才能加载活动。没有一次点击的一致性。似乎存在什么问题?
android:clickable="false"
。 - karan