欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android动态修改ToolBar的Menu菜单示例

程序员文章站 2024-02-11 20:35:22
android动态修改toolbar的menu菜单 效果图 实现 实现很简单,就是一个具有3个action的menu,在我们滑动到不同状态的时候,把对应的acti...

android动态修改toolbar的menu菜单

效果图

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;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。