我有一个带有底部菜单导航栏的Android Activity,它在三个片段和一个应该打开新活动的顶级菜单项之间导航,但顶级菜单ActionBar项目没有响应点击事件。也许有些东西不见了?或者我应该从我的片段中处理菜单?
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set Network Connection Listener
setConnectionListener(this);
//check the network connectivity when activity is created
checkConnection();
BottomNavigationView bottomNavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigation.setOnNavigationItemSelectedListener(this);
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, HomeFragment.newInstance());
transaction.commit();
// Used to select item programmatically
// bottomNavigation.getMenu().getItem(0).setChecked(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.top, menu);
// return super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, "Menu Clicked " + item.getItemId());
switch (item.getItemId()){
case R.id.tab_cart:
Intent intent = new Intent(this, CartActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
我的菜单top.xml res菜单文件
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.shopcentra.activities.MainActivity">
<item
android:id="@+id/tab_cart"
android:icon="@mipmap/cart"
android:title="@string/cart"
app:actionLayout="@layout/notification_layout"
app:showAsAction="always">
</item>
</menu>
答案 0 :(得分:2)
我无法在&#34; tab_cart&#34;上看到您添加了View.OnClickListener的位置。如果您已经设置了一个,请分享该代码,否则,在该视图上添加一个OnCLickListener,然后查看问题是否仍然存在。
编辑:在菜单项上添加OnClickListener的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem tabCartMenuItem = menu.findItem(R.id.tab_cart);
View notificationActionView = menuItem.getActionView();
notificationActionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(tabCartMenuItem));
}
});
}
尝试将此代码添加到onCreateOptionsMenu()方法。