@Override
  public void updateMenuView(boolean cleared) {
    super.updateMenuView(cleared);

    if (mMenu != null) {
      final ArrayList<MenuItemImpl> actionItems = mMenu.getActionItems();
      final int count = actionItems.size();
      for (int i = 0; i < count; i++) {
        final ActionProvider provider = actionItems.get(i).getActionProvider();
        if (provider != null) {
          provider.setSubUiVisibilityListener(this);
        }
      }
    }

    final ArrayList<MenuItemImpl> nonActionItems = mMenu != null ? mMenu.getNonActionItems() : null;

    boolean hasOverflow = false;
    if (mReserveOverflow && nonActionItems != null) {
      final int count = nonActionItems.size();
      if (count == 1) {
        hasOverflow = !nonActionItems.get(0).isActionViewExpanded();
      } else {
        hasOverflow = count > 0;
      }
    }

    if (hasOverflow) {
      if (mOverflowButton == null) {
        mOverflowButton = new OverflowMenuButton(mSystemContext);
      }
      ViewGroup parent = (ViewGroup) mOverflowButton.getParent();
      if (parent != mMenuView) {
        if (parent != null) {
          parent.removeView(mOverflowButton);
        }
        ActionMenuView menuView = (ActionMenuView) mMenuView;
        menuView.addView(mOverflowButton, menuView.generateOverflowButtonLayoutParams());
      }
    } else if (mOverflowButton != null && mOverflowButton.getParent() == mMenuView) {
      ((ViewGroup) mMenuView).removeView(mOverflowButton);
    }

    ((ActionMenuView) mMenuView).setOverflowReserved(mReserveOverflow);
  }
  @Override
  public View getItemView(MenuItemImpl item, View convertView, ViewGroup parent) {
    View actionView = item.getActionView();
    if (actionView == null || item.hasCollapsibleActionView()) {
      if (!(convertView instanceof ActionMenuItemView)) {
        convertView = null;
      }
      actionView = super.getItemView(item, convertView, parent);
    }
    actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE);

    final ActionMenuView menuParent = (ActionMenuView) parent;
    final ViewGroup.LayoutParams lp = actionView.getLayoutParams();
    if (!menuParent.checkLayoutParams(lp)) {
      actionView.setLayoutParams(menuParent.generateLayoutParams(lp));
    }
    return actionView;
  }
示例#3
0
 public void animateToVisibility(int visibility) {
   if (mVisibilityAnim != null) {
     mVisibilityAnim.cancel();
   }
   if (visibility == VISIBLE) {
     if (getVisibility() != VISIBLE) {
       setAlpha(0);
       if (mSplitView != null && mMenuView != null) {
         mMenuView.setAlpha(0);
       }
     }
     ObjectAnimator anim = ObjectAnimator.ofFloat(this, "alpha", 1);
     anim.setDuration(FADE_DURATION);
     anim.setInterpolator(sAlphaInterpolator);
     if (mSplitView != null && mMenuView != null) {
       AnimatorSet set = new AnimatorSet();
       ObjectAnimator splitAnim = ObjectAnimator.ofFloat(mMenuView, "alpha", 1);
       splitAnim.setDuration(FADE_DURATION);
       set.addListener(mVisAnimListener.withFinalVisibility(visibility));
       set.play(anim).with(splitAnim);
       set.start();
     } else {
       anim.addListener(mVisAnimListener.withFinalVisibility(visibility));
       anim.start();
     }
   } else {
     ObjectAnimator anim = ObjectAnimator.ofFloat(this, "alpha", 0);
     anim.setDuration(FADE_DURATION);
     anim.setInterpolator(sAlphaInterpolator);
     if (mSplitView != null && mMenuView != null) {
       AnimatorSet set = new AnimatorSet();
       ObjectAnimator splitAnim = ObjectAnimator.ofFloat(mMenuView, "alpha", 0);
       splitAnim.setDuration(FADE_DURATION);
       set.addListener(mVisAnimListener.withFinalVisibility(visibility));
       set.play(anim).with(splitAnim);
       set.start();
     } else {
       anim.addListener(mVisAnimListener.withFinalVisibility(visibility));
       anim.start();
     }
   }
 }
示例#4
0
  /*
   * Must be public so we can dispatch pre-2.2 via ActionBarImpl.
   */
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
      super.onConfigurationChanged(newConfig);
    } else if (mMenuView != null) {
      mMenuView.onConfigurationChanged(newConfig);
    }

    // Action bar can change size on configuration changes.
    // Reread the desired height from the theme-specified style.
    TypedArray a =
        getContext()
            .obtainStyledAttributes(null, R.styleable.SherlockActionBar, R.attr.actionBarStyle, 0);
    setContentHeight(a.getLayoutDimension(R.styleable.SherlockActionBar_height, 0));
    a.recycle();
    if (mSplitWhenNarrow) {
      setSplitActionBar(
          getResources_getBoolean(getContext(), R.bool.abs__split_action_bar_is_narrow));
    }
    if (mActionMenuPresenter != null) {
      mActionMenuPresenter.onConfigurationChanged(newConfig);
    }
  }
  public boolean flagActionItems() {
    final ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems();
    final int itemsSize = visibleItems.size();
    int maxActions = mMaxItems;
    int widthLimit = mActionItemWidthLimit;
    final int querySpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final ViewGroup parent = (ViewGroup) mMenuView;

    int requiredItems = 0;
    int requestedItems = 0;
    int firstActionWidth = 0;
    boolean hasOverflow = false;
    for (int i = 0; i < itemsSize; i++) {
      MenuItemImpl item = visibleItems.get(i);
      if (item.requiresActionButton()) {
        requiredItems++;
      } else if (item.requestsActionButton()) {
        requestedItems++;
      } else {
        hasOverflow = true;
      }
      if (mExpandedActionViewsExclusive && item.isActionViewExpanded()) {
        // Overflow everything if we have an expanded action view and we're
        // space constrained.
        maxActions = 0;
      }
    }

    // Reserve a spot for the overflow item if needed.
    if (mReserveOverflow && (hasOverflow || requiredItems + requestedItems > maxActions)) {
      maxActions--;
    }
    maxActions -= requiredItems;

    final SparseBooleanArray seenGroups = mActionButtonGroups;
    seenGroups.clear();

    int cellSize = 0;
    int cellsRemaining = 0;
    if (mStrictWidthLimit) {
      cellsRemaining = widthLimit / mMinCellSize;
      final int cellSizeRemaining = widthLimit % mMinCellSize;
      cellSize = mMinCellSize + cellSizeRemaining / cellsRemaining;
    }

    // Flag as many more requested items as will fit.
    for (int i = 0; i < itemsSize; i++) {
      MenuItemImpl item = visibleItems.get(i);

      if (item.requiresActionButton()) {
        View v = getItemView(item, mScrapActionButtonView, parent);
        if (mScrapActionButtonView == null) {
          mScrapActionButtonView = v;
        }
        if (mStrictWidthLimit) {
          cellsRemaining -=
              ActionMenuView.measureChildForCells(v, cellSize, cellsRemaining, querySpec, 0);
        } else {
          v.measure(querySpec, querySpec);
        }
        final int measuredWidth = v.getMeasuredWidth();
        widthLimit -= measuredWidth;
        if (firstActionWidth == 0) {
          firstActionWidth = measuredWidth;
        }
        final int groupId = item.getGroupId();
        if (groupId != 0) {
          seenGroups.put(groupId, true);
        }
        item.setIsActionButton(true);
      } else if (item.requestsActionButton()) {
        // Items in a group with other items that already have an action slot
        // can break the max actions rule, but not the width limit.
        final int groupId = item.getGroupId();
        final boolean inGroup = seenGroups.get(groupId);
        boolean isAction =
            (maxActions > 0 || inGroup)
                && widthLimit > 0
                && (!mStrictWidthLimit || cellsRemaining > 0);

        if (isAction) {
          View v = getItemView(item, mScrapActionButtonView, parent);
          if (mScrapActionButtonView == null) {
            mScrapActionButtonView = v;
          }
          if (mStrictWidthLimit) {
            final int cells =
                ActionMenuView.measureChildForCells(v, cellSize, cellsRemaining, querySpec, 0);
            cellsRemaining -= cells;
            if (cells == 0) {
              isAction = false;
            }
          } else {
            v.measure(querySpec, querySpec);
          }
          final int measuredWidth = v.getMeasuredWidth();
          widthLimit -= measuredWidth;
          if (firstActionWidth == 0) {
            firstActionWidth = measuredWidth;
          }

          if (mStrictWidthLimit) {
            isAction &= widthLimit >= 0;
          } else {
            // Did this push the entire first item past the limit?
            isAction &= widthLimit + firstActionWidth > 0;
          }
        }

        if (isAction && groupId != 0) {
          seenGroups.put(groupId, true);
        } else if (inGroup) {
          // We broke the width limit. Demote the whole group, they all overflow now.
          seenGroups.put(groupId, false);
          for (int j = 0; j < i; j++) {
            MenuItemImpl areYouMyGroupie = visibleItems.get(j);
            if (areYouMyGroupie.getGroupId() == groupId) {
              // Give back the action slot
              if (areYouMyGroupie.isActionButton()) maxActions++;
              areYouMyGroupie.setIsActionButton(false);
            }
          }
        }

        if (isAction) maxActions--;

        item.setIsActionButton(isAction);
      }
    }
    return true;
  }
 @Override
 public MenuView getMenuView(ViewGroup root) {
   MenuView result = super.getMenuView(root);
   ((ActionMenuView) result).setPresenter(this);
   return result;
 }
示例#7
0
  public void setMenu(Menu menu, MenuPresenter.Callback cb) {
    if (menu == mOptionsMenu) return;

    if (mOptionsMenu != null) {
      mOptionsMenu.removeMenuPresenter(mActionMenuPresenter);
      mOptionsMenu.removeMenuPresenter(mExpandedMenuPresenter);
    }

    MenuBuilder builder = (MenuBuilder) menu;
    mOptionsMenu = builder;
    if (mMenuView != null) {
      final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
      if (oldParent != null) {
        oldParent.removeView(mMenuView);
      }
    }
    if (mActionMenuPresenter == null) {
      mActionMenuPresenter = new ActionMenuPresenter(mContext);
      mActionMenuPresenter.setCallback(cb);
      mActionMenuPresenter.setId(R.id.abs__action_menu_presenter);
      mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
    }

    ActionMenuView menuView;
    final LayoutParams layoutParams =
        new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    if (!mSplitActionBar) {
      mActionMenuPresenter.setExpandedActionViewsExclusive(
          getResources_getBoolean(
              getContext(), R.bool.abs__action_bar_expanded_action_views_exclusive));
      configPresenters(builder);
      menuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
      final ViewGroup oldParent = (ViewGroup) menuView.getParent();
      if (oldParent != null && oldParent != this) {
        oldParent.removeView(menuView);
      }
      addView(menuView, layoutParams);
    } else {
      mActionMenuPresenter.setExpandedActionViewsExclusive(false);
      // Allow full screen width in split mode.
      mActionMenuPresenter.setWidthLimit(
          getContext().getResources().getDisplayMetrics().widthPixels, true);
      // No limit to the item count; use whatever will fit.
      mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
      // Span the whole width
      layoutParams.width = LayoutParams.MATCH_PARENT;
      configPresenters(builder);
      menuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
      if (mSplitView != null) {
        final ViewGroup oldParent = (ViewGroup) menuView.getParent();
        if (oldParent != null && oldParent != mSplitView) {
          oldParent.removeView(menuView);
        }
        menuView.setVisibility(getAnimatedVisibility());
        mSplitView.addView(menuView, layoutParams);
      } else {
        // We'll add this later if we missed it this time.
        menuView.setLayoutParams(layoutParams);
      }
    }
    mMenuView = menuView;
  }