コード例 #1
0
  public void initForMode(final ActionMode mode) {
    if (mClose == null) {
      LayoutInflater inflater = LayoutInflater.from(getContext());
      mClose = inflater.inflate(mCloseItemLayout, this, false);
      addView(mClose);
    } else if (mClose.getParent() == null) {
      addView(mClose);
    }

    View closeButton = mClose.findViewById(R.id.action_mode_close_button);
    closeButton.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            mode.finish();
          }
        });

    final MenuBuilder menu = (MenuBuilder) mode.getMenu();
    if (mActionMenuPresenter != null) {
      mActionMenuPresenter.dismissPopupMenus();
    }
    mActionMenuPresenter = new ActionMenuPresenter(getContext());
    mActionMenuPresenter.setReserveOverflow(true);

    final LayoutParams layoutParams =
        new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    menu.addMenuPresenter(mActionMenuPresenter, mPopupContext);
    mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
    mMenuView.setBackgroundDrawable(null);
    addView(mMenuView, layoutParams);
  }
コード例 #2
0
  /**
   * Switches between "mark as read" and "mark as unread" buttons for the action mode depending on
   * which items in the RecyclerView are selected.
   */
  private void updateButtons() {
    int read = 0;
    int unread = 0;
    for (int i = 0; i < adapter.getItemCount(); i++) {
      if (adapter.isItemChecked(i)) {
        if (adapter.getItem(i).isUnread()) {
          unread++;
        } else {
          read++;
        }
      }
    }

    MenuItem item = actionMode.getMenu().findItem(R.id.mark_read_unread_button);
    if (read > unread) {
      item.setIcon(R.drawable.ic_markunread_white_24dp);
      item.setTitle(R.string.conversations_action_mark_unread);
    } else {
      item.setIcon(R.drawable.ic_drafts_white_24dp);
      item.setTitle(R.string.conversations_action_mark_read);
    }
  }
  ActionMode startSupportActionModeFromWindow(ActionMode.Callback callback) {
    if (mActionMode != null) {
      mActionMode.finish();
    }

    final ActionMode.Callback wrappedCallback = new ActionModeCallbackWrapper(callback);

    if (mActionModeView == null) {
      // Use the action bar theme.
      final Context actionBarContext;
      actionBarContext =
          ThemeUtils.getActionBarThemedContext(
              mActivity, mThemed.getCurrentThemeResourceId(), mThemed.getCurrentThemeColor());

      mActionModeView = new ActionBarContextView(actionBarContext);
      mActionModePopup =
          new PopupWindow(
              actionBarContext,
              null,
              android.support.v7.appcompat.R.attr.actionModePopupWindowStyle);
      mActionModePopup.setContentView(mActionModeView);

      final TypedValue outValue = new TypedValue();
      actionBarContext
          .getTheme()
          .resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, outValue, true);
      final int height =
          TypedValue.complexToDimensionPixelSize(
              outValue.data, actionBarContext.getResources().getDisplayMetrics());
      mActionModeView.setContentHeight(height);
      ThemeUtils.setActionBarContextViewBackground(
          mActionModeView,
          mThemed.getCurrentThemeResourceId(),
          mThemed.getCurrentThemeColor(),
          mThemed.getCurrentThemeBackgroundOption(),
          false);
      mActionModePopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
      final Rect actionModeBounds = getActionModeBounds();
      if (actionModeBounds != null) {
        mActionModePopup.setWidth(actionModeBounds.width());
      } else {
        mActionModePopup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
      }
      mShowActionModePopup =
          new Runnable() {
            @Override
            public void run() {
              if (actionModeBounds != null) {
                mActionModePopup.showAtLocation(
                    mWindow.getDecorView(),
                    Gravity.TOP | Gravity.LEFT,
                    actionModeBounds.left,
                    actionModeBounds.top);
              } else {
                mActionModePopup.showAtLocation(
                    mWindow.getDecorView(), Gravity.TOP | Gravity.LEFT, 0, 0);
              }
            }
          };
    }

    if (mActionModeView != null) {
      mActionModeView.killMode();
      ActionMode mode =
          new StandaloneActionMode(
              mActionModeView.getContext(),
              mActionModeView,
              wrappedCallback,
              mActionModePopup == null);
      if (callback.onCreateActionMode(mode, mode.getMenu())) {
        mode.invalidate();
        mActionModeView.initForMode(mode);
        mActionModeView.setVisibility(View.VISIBLE);
        mActionMode = mode;
        if (mActionModePopup != null) {
          mWindow.getDecorView().post(mShowActionModePopup);
        }
        mActionModeView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);

        if (mActionModeView.getParent() != null) {
          ViewCompat.requestApplyInsets((View) mActionModeView.getParent());
        }
      } else {
        mActionMode = null;
      }
    }
    if (mActionMode != null && mAppCompatCallback != null) {
      mAppCompatCallback.onSupportActionModeStarted(mActionMode);
    }
    return mActionMode;
  }