• Creating Contextual Menus创建上下文菜单


    A contextual menu offers actions that affect a specific item or context frame in the UI. You can provide a context menu for any view, but they are most often used for items in a ListViewGridView, or other view collections in which the user can perform direct actions on each item.

    一个上下文菜单提供了行动,影响特定项目或上下文框架在UI。你可以提供一个上下文菜单,任何观点,但他们通常都用于项目在列表视图中,显示数据表格,或其他视图集合中,用户可以直接在每个项目上执行操作。

    There are two ways to provide contextual actions:

    有两种方法提供相关操作:

    • In a floating context menu. A menu appears as a floating list of menu items (similar to a dialog) when the user performs a long-click (press and hold) on a view that declares support for a context menu. Users can perform a contextual action on one item at a time.
    • In the contextual action mode. This mode is a system implementation ofActionMode that displays a contextual action bar at the top of the screen with action items that affect the selected item(s). When this mode is active, users can perform an action on multiple items at once (if your app allows it).

    Note: The contextual action mode is available on Android 3.0 (API level 11) and higher and is the preferred technique for displaying contextual actions when available. If your app supports versions lower than 3.0 then you should fall back to a floating context menu on those devices.

    在一个浮动的上下文菜单。一个菜单显示为一个浮动菜单项列表(类似于一个对话框),在用户执行一个长点击(按下并保持住)在一个视图,宣布支持一个上下文菜单。用户可以执行上下文动作在一个项目在一个时间。   在上下文动作模式。这种模式是一个系统实现的ActionMode上下文操作栏显示在屏幕顶端与行动项目,影响选择的项目(s)。当这个模式被激活时,用户可以执行一个动作在多个项目在一次(如果你的应用程序允许它)。   注意:上下文动作模式是可以在Android 3.0(API级别11)和更高的和是首选的技术显示相关操作可用时。如果你的应用程序支持版本低于3.0,那么你应该回退到一个浮动的上下文菜单在那些设备。


    Creating a floating context menu创建一个浮动的上下文菜单

    To provide a floating context menu:

    1. Register the View to which the context menu should be associated by calling registerForContextMenu() and pass it the View.

      If your activity uses a ListView or GridView and you want each item to provide the same context menu, register all items for a context menu by passing the ListView or GridView to registerForContextMenu().

    2. Implement the onCreateContextMenu() method in your Activity or Fragment.

      When the registered view receives a long-click event, the system calls your onCreateContextMenu() method. This is where you define the menu items, usually by inflating a menu resource. For example:

      @Override
      public void onCreateContextMenu(ContextMenu menu, View v,
                                      ContextMenuInfo menuInfo) {
          super.onCreateContextMenu(menu, v, menuInfo);
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.context_menu, menu);
      }

      MenuInflater allows you to inflate the context menu from a menu resource. The callback method parameters include the View that the user selected and a ContextMenu.ContextMenuInfo object that provides additional information about the item selected. If your activity has several views that each provide a different context menu, you might use these parameters to determine which context menu to inflate.

    3. Implement onContextItemSelected().

      When the user selects a menu item, the system calls this method so you can perform the appropriate action. For example:

      @Override
      public boolean onContextItemSelected(MenuItem item) {
          AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
          switch (item.getItemId()) {
              case R.id.edit:
                  editNote(info.id);
                  return true;
              case R.id.delete:
                  deleteNote(info.id);
                  return true;
              default:
                  return super.onContextItemSelected(item);
          }
      }

      The getItemId() method queries the ID for the selected menu item, which you should assign to each menu item in XML using the android:id attribute, as shown in the section about Defining a Menu in XML.

      When you successfully handle a menu item, return true. If you don't handle the menu item, you should pass the menu item to the superclass(超类) implementation. If your activity includes fragments, the activity receives this callback first. By calling the superclass when unhandled, the system passes the event to the respective callback method in each fragment, one at a time (in the order each fragment was added) until trueor false is returned. (The default implementation for Activity and android.app.Fragment return false, so you should always call the superclass(超类) when unhandled.)


  • 相关阅读:
    mount命令详解
    traceroute命令详解
    etcd节点扩容至两个节点
    shell历史命令
    etcd单节点安装
    linux中修改环境变量及生效方法
    ansible最佳实战部署nginx
    用roles部署nginx
    playbook部署mangodb
    安装mangodb
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215658.html
Copyright © 2020-2023  润新知