Я создал пользовательский макет для пункта меню. Теперь мне нужно, чтобы он загружал новое действие при нажатии. Логика код выглядит следующим образом:пользовательский элемент меню действий не отвечает на один клик
декларацию меню в ресурсах:
<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, мне нужно позвонить setHasOptionsMenu(true);
внутри метода onCreateView
внутри фрагмента, и я это сделал.
Теперь внутри деятельности, 2 основных важные методы, соответственно 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
}
}
Проблема заключается в следующем: при нажатии на пункт меню в ActionBar, он работает довольно "случайно". Иногда он будет работать после одного щелчка, иначе он загрузит 4 клика или 3 клика. Не существует последовательности 1 клик. В чем проблема?
попробуйте удалить 'android: clickable =" false "' –