下面列出了怎么用androidx.appcompat.widget.Toolbar.OnMenuItemClickListener的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void initData() {
setToolBar(mToolBar, "");
tvAcgDetailLabels.setVisibility(android.view.View.GONE);
mSHNewsPostItem = getIntent().getParcelableExtra(IntentConstant.ISH_NEWS_ITEM);
if (mSHNewsPostItem == null) {
showError(R.string.msg_error_unknown);
return;
}
mToolBar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.acginfo_share) {
mPresenter.start2Share(new RxPermissions(ISHNewsDetailActivity.this));
}
return false;
}
});
mPresenter.getNewsDetail(mSHNewsPostItem.getId());
}
@Override
protected void initData() {
setToolBar(mToolBar, "");
mZeroFiveNewsItem = getIntent().getParcelableExtra(IntentConstant.ZERO_FIVE_NEWS_ITEM);
if (mZeroFiveNewsItem == null) {
showError(R.string.msg_error_unknown);
return;
}
mToolBar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.acginfo_share) {
mPresenter.start2Share(new RxPermissions(ZeroFiveNewsDetailActivity.this));
}
return false;
}
});
mPresenter.getNewsDetail(mZeroFiveNewsItem.getContentLink());
}
/**
* Creates and returns a listener, which allows to observe, when an item of the tab switcher's
* toolbar has been clicked.
*
* @return The listener, which has been created, as an instance of the type {@link
* OnMenuItemClickListener}. The listener may not be null
*/
@NonNull
private OnMenuItemClickListener createToolbarMenuListener() {
return new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final MenuItem item) {
switch (item.getItemId()) {
case R.id.remove_tab_menu_item:
Tab selectedTab = tabSwitcher.getSelectedTab();
if (selectedTab != null) {
tabSwitcher.removeTab(selectedTab);
}
return true;
case R.id.add_tab_menu_item:
int index = tabSwitcher.getCount();
Tab tab = createTab(index);
if (tabSwitcher.isSwitcherShown()) {
tabSwitcher.addTab(tab, 0, createRevealAnimation());
} else {
tabSwitcher.addTab(tab, 0, createPeekAnimation());
}
return true;
case R.id.clear_tabs_menu_item:
tabSwitcher.clear();
return true;
case R.id.settings_menu_item:
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
default:
return false;
}
}
};
}
@Override
public final void inflateToolbarMenu(@MenuRes final int resourceId,
@Nullable final OnMenuItemClickListener listener) {
this.toolbarMenuId = resourceId;
this.toolbarMenuItemListener = listener;
notifyOnToolbarMenuInflated(resourceId, listener);
}
@Override
public final void inflateToolbarMenu(@MenuRes final int resourceId,
@Nullable final OnMenuItemClickListener listener) {
model.inflateToolbarMenu(resourceId, listener);
}
@Override
public final void onToolbarMenuInflated(@MenuRes final int resourceId,
@Nullable final OnMenuItemClickListener listener) {
inflateToolbarMenu();
}
/**
* Notifies the listeners, that the menu of the toolbar, which is shown, when the tab switcher
* is shown, has been inflated.
*
* @param resourceId
* The resource id of the menu, which has been inflated, as an {@link Integer} value.
* The resource id must correspond to a valid menu resource
* @param menuItemClickListener
* The listener, which has been registered to be notified, when an item of the menu has
* been clicked, as an instance of the type OnMenuItemClickListener or null, if no
* listener should be notified
*/
private void notifyOnToolbarMenuInflated(@MenuRes final int resourceId,
@Nullable final OnMenuItemClickListener menuItemClickListener) {
for (Listener listener : listeners) {
listener.onToolbarMenuInflated(resourceId, menuItemClickListener);
}
}
/**
* The method, which is invoked, when the menu of the toolbar, which is shown, when the tab
* switcher is shown, has been inflated.
*
* @param resourceId
* The resource id of the menu, which has been inflated, as an {@link Integer}
* value. The resource id must correspond to a valid menu resource
* @param listener
* The listener, which has been registered to be notified, when an item of the menu
* has been clicked, as an instance of the type OnMenuItemClickListener or null, if
* no listener should be notified
*/
void onToolbarMenuInflated(@MenuRes int resourceId,
@Nullable OnMenuItemClickListener listener);
/**
* Inflates the menu of the toolbar, which is shown, when the tab switcher is shown. When using
* the tablet layout, the menu is inflated into the secondary toolbar. If another menu has
* already been inflated, its items will be removed.
*
* @param resourceId
* The resource id of the menu, which should be inflated, as an {@link Integer} value.
* The resource id must correspond to a valid menu resource
* @param listener
* The listener, which should be notified, when an menu item has been clicked, as an
* instance of the type OnMenuItemClickListener or null, if no listener should be
* notified
*/
void inflateToolbarMenu(@MenuRes int resourceId, @Nullable OnMenuItemClickListener listener);
/**
* Returns the listener, which is notified, when an item of the menu of the toolbar, which is
* shown, when the tab switcher is shown, has been clicked.
*
* @return The listener, which is notified, when an item of the menu of the toolbar, which is
* shown, when the tab switcher is shown, has been clicked as an instance of the type
* OnMenuItemClickListener or null, if no listener should be notified
*/
@Nullable
public final OnMenuItemClickListener getToolbarMenuItemListener() {
return toolbarMenuItemListener;
}