Android动态修改ToolBar的Menu菜单示例
作者:kqw攻城狮 时间:2021-10-29 15:36:07
Android动态修改ToolBar的Menu菜单
效果图
实现
实现很简单,就是一个具有3个Action的Menu,在我们滑动到不同状态的时候,把对应的Action隐藏了。
开始上货
Menu
Menu下添加3个Item
<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.kongqw.myapplication.MainActivity">
<item
android:id="@+id/search_button"
android:icon="@mipmap/navigation_icon_search"
android:orderInCategory="100"
android:title="搜索"
app:showAsAction="ifRoom" />
<item
android:id="@+id/scan_button"
android:icon="@mipmap/navigation_icon_sweep"
android:orderInCategory="100"
android:title="扫描"
app:showAsAction="ifRoom" />
<item
android:id="@+id/setting_button"
android:icon="@mipmap/titlebar_icon_settings_normal"
android:orderInCategory="100"
android:title="设置"
app:showAsAction="ifRoom" />
</menu>
重写onPrepareOptionsMenu方法
用来根据ViewPager显示页面控制Menu显示的Item
Demo里我用的是ViewPager滑动来控制,可以根据自己需求,不过大多数都是判断ViewPager吧。
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// 动态设置ToolBar状态
switch (mViewPager.getCurrentItem()) {
case 0:
menu.findItem(R.id.search_button).setVisible(true);
menu.findItem(R.id.scan_button).setVisible(true);
menu.findItem(R.id.setting_button).setVisible(false);
break;
case 1:
menu.findItem(R.id.search_button).setVisible(false);
menu.findItem(R.id.scan_button).setVisible(false);
menu.findItem(R.id.setting_button).setVisible(false);
break;
case 2:
menu.findItem(R.id.search_button).setVisible(false);
menu.findItem(R.id.scan_button).setVisible(false);
menu.findItem(R.id.setting_button).setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
Item点击
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.search_button) {
Toast.makeText(getApplicationContext(), "搜索", Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.scan_button) {
Toast.makeText(getApplicationContext(), "扫描", Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.setting_button) {
Toast.makeText(getApplicationContext(), "设置", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
监听ViewPager滑动,改变Menu状态(重点)
调用invalidateOptionsMenu();方法从新加载Menu,即回调onPrepareOptionsMenu方法
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// getWindow().invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
invalidateOptionsMenu();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Code(参考)
就是Android Studio默认创建的带有ViewPager的Demo,
package com.kongqw.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// getWindow().invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
invalidateOptionsMenu();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// 动态设置ToolBar状态
switch (mViewPager.getCurrentItem()) {
case 0:
menu.findItem(R.id.search_button).setVisible(true);
menu.findItem(R.id.scan_button).setVisible(true);
menu.findItem(R.id.setting_button).setVisible(false);
break;
case 1:
menu.findItem(R.id.search_button).setVisible(false);
menu.findItem(R.id.scan_button).setVisible(false);
menu.findItem(R.id.setting_button).setVisible(false);
break;
case 2:
menu.findItem(R.id.search_button).setVisible(false);
menu.findItem(R.id.scan_button).setVisible(false);
menu.findItem(R.id.setting_button).setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.search_button) {
Toast.makeText(getApplicationContext(), "搜索", Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.scan_button) {
Toast.makeText(getApplicationContext(), "扫描", Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.setting_button) {
Toast.makeText(getApplicationContext(), "设置", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
来源:http://blog.csdn.net/q4878802/article/details/51160424
标签:toolbar,android
0
投稿
猜你喜欢
Mybatis动态调用表名和字段名的解决方法
2022-03-18 16:54:14
java处理图片背景颜色的方法
2023-11-27 04:38:20
Springboot 如何指定获取出 yml文件里面的配置值
2022-08-29 21:04:48
说说Android的UI刷新机制的实现
2022-11-26 22:34:21
解读赫夫曼树编码的问题
2022-03-13 06:37:26
Spring Bean后处理器详细介绍
2021-06-27 07:29:06
View触发机制API实现GestureDetector OverScroller详解
2023-01-24 00:57:08
BootStrap mvcpager分页样式(get请求,刷新页面)
2023-09-19 04:18:29
Spring生命周期回调与容器扩展详解
2023-04-05 16:26:31
解析Spring Boot内嵌tomcat关于getServletContext().getRealPath获取得到临时路径的问题
2023-08-28 07:44:11
java利用Future实现多线程执行与结果聚合实例代码
2023-09-24 11:07:18
java LRU(Least Recently Used )详解及实例代码
2022-10-08 10:42:43
Java桶排序之基数排序详解
2021-08-26 13:38:58
详解SpringMVC的url-pattern配置及原理剖析
2023-08-11 12:00:27
C#开发WinForm根据条件改变DataGridView行颜色
2022-05-06 05:38:56
全网最详细Hutool工具详解
2023-05-04 06:21:53
SpringBoot向容器注册bean的方法详解
2023-09-02 10:58:22
详解springboot项目带Tomcat和不带Tomcat的两种打包方式
2023-11-28 08:23:41
C# ComboBox的联动操作(三层架构)
2022-06-21 16:37:56
C#获取Word文档中所有表格的实现代码分享
2023-05-12 10:01:46