Search in sources :

Example 6 with MenuItemImpl

use of android.support.v7.view.menu.MenuItemImpl in project floatingsearchview by arimorty.

the class MenuView method reset.

/**
 * Resets the the view to fit into a new
 * available width.
 * <p/>
 * <p>This clears and then re-inflates the menu items
 * , removes all of its associated action views, and re-creates
 * the menu and action items to fit in the new width.</p>
 *
 * @param availWidth the width available for the menu to use. If
 *                   there is room, menu items that are flagged with
 *                   android:showAsAction="ifRoom" or android:showAsAction="always"
 *                   will show as actions.
 */
public void reset(int menu, int availWidth) {
    mMenu = menu;
    if (mMenu == -1) {
        return;
    }
    mActionShowAlwaysItems = new ArrayList<>();
    mActionItems = new ArrayList<>();
    mMenuItems = new ArrayList<>();
    mMenuBuilder = new MenuBuilder(getContext());
    mMenuPopupHelper = new MenuPopupHelper(getContext(), mMenuBuilder, this);
    // clean view and re-inflate
    removeAllViews();
    getMenuInflater().inflate(mMenu, mMenuBuilder);
    mMenuItems = mMenuBuilder.getActionItems();
    mMenuItems.addAll(mMenuBuilder.getNonActionItems());
    Collections.sort(mMenuItems, new Comparator<MenuItemImpl>() {

        @Override
        public int compare(MenuItemImpl lhs, MenuItemImpl rhs) {
            return ((Integer) lhs.getOrder()).compareTo(rhs.getOrder());
        }
    });
    List<MenuItemImpl> localActionItems = filter(mMenuItems, new MenuItemImplPredicate() {

        @Override
        public boolean apply(MenuItemImpl menuItem) {
            return menuItem.getIcon() != null && (menuItem.requiresActionButton() || menuItem.requestsActionButton());
        }
    });
    int availItemRoom = availWidth / (int) ACTION_DIMENSION_PX;
    // determine if to show overflow menu
    boolean addOverflowAtTheEnd = false;
    if (((localActionItems.size() < mMenuItems.size()) || availItemRoom < localActionItems.size())) {
        addOverflowAtTheEnd = true;
        availItemRoom--;
    }
    ArrayList<Integer> actionItemsIds = new ArrayList<>();
    if (availItemRoom > 0) {
        for (int i = 0; i < localActionItems.size(); i++) {
            final MenuItemImpl menuItem = localActionItems.get(i);
            if (menuItem.getIcon() != null) {
                ImageView action = createActionView();
                action.setContentDescription(menuItem.getTitle());
                action.setImageDrawable(menuItem.getIcon());
                Util.setIconColor(action, mActionIconColor);
                addView(action);
                mActionItems.add(menuItem);
                actionItemsIds.add(menuItem.getItemId());
                action.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mMenuCallback != null) {
                            mMenuCallback.onMenuItemSelected(mMenuBuilder, menuItem);
                        }
                    }
                });
                availItemRoom--;
                if (availItemRoom == 0) {
                    break;
                }
            }
        }
    }
    mHasOverflow = addOverflowAtTheEnd;
    if (addOverflowAtTheEnd) {
        ImageView overflowAction = getOverflowActionView();
        overflowAction.setImageResource(R.drawable.ic_more_vert_black_24dp);
        Util.setIconColor(overflowAction, mOverflowIconColor);
        addView(overflowAction);
        overflowAction.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mMenuPopupHelper.show();
            }
        });
        mMenuBuilder.setCallback(mMenuCallback);
    }
    // remove all menu items that will be shown as icons (the action items) from the overflow menu
    for (int id : actionItemsIds) {
        mMenuBuilder.removeItem(id);
    }
    actionItemsIds = null;
    if (mOnVisibleWidthChangedListener != null) {
        mVisibleWidth = ((int) ACTION_DIMENSION_PX * getChildCount()) - (mHasOverflow ? Util.dpToPx(8) : 0);
        mOnVisibleWidthChangedListener.onItemsMenuVisibleWidthChanged(mVisibleWidth);
    }
}
Also used : MenuPopupHelper(com.arlib.floatingsearchview.util.MenuPopupHelper) ArrayList(java.util.ArrayList) MenuBuilder(android.support.v7.view.menu.MenuBuilder) ImageView(android.widget.ImageView) View(android.view.View) MenuItemImpl(android.support.v7.view.menu.MenuItemImpl) ImageView(android.widget.ImageView)

Example 7 with MenuItemImpl

use of android.support.v7.view.menu.MenuItemImpl in project HoloEverywhere by Prototik.

the class ActionMenuPresenter method getItemView.

@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;
}
Also used : ViewGroup(android.view.ViewGroup) ActionMenuChildView(android.support.v7.internal.view.menu.ActionMenuView.ActionMenuChildView) View(android.view.View)

Example 8 with MenuItemImpl

use of android.support.v7.view.menu.MenuItemImpl in project HoloEverywhere by Prototik.

the class ActionMenuPresenter method flagActionItems.

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;
}
Also used : ViewGroup(android.view.ViewGroup) SparseBooleanArray(android.util.SparseBooleanArray) ActionMenuChildView(android.support.v7.internal.view.menu.ActionMenuView.ActionMenuChildView) View(android.view.View)

Example 9 with MenuItemImpl

use of android.support.v7.view.menu.MenuItemImpl in project iosched by google.

the class BottomNavigationMenu method addInternal.

@Override
protected MenuItem addInternal(int group, int id, int categoryOrder, CharSequence title) {
    if (size() + 1 > MAX_ITEM_COUNT) {
        throw new IllegalArgumentException("Maximum number of items supported by BottomNavigationView is " + MAX_ITEM_COUNT + ". Limit can be checked with BottomNavigationView#getMaxItemCount()");
    }
    stopDispatchingItemsChanged();
    final MenuItem item = super.addInternal(group, id, categoryOrder, title);
    if (item instanceof MenuItemImpl) {
        ((MenuItemImpl) item).setExclusiveCheckable(true);
    }
    startDispatchingItemsChanged();
    return item;
}
Also used : MenuItemImpl(android.support.v7.view.menu.MenuItemImpl) MenuItem(android.view.MenuItem)

Aggregations

MenuItemImpl (android.support.v7.view.menu.MenuItemImpl)7 View (android.view.View)5 ImageView (android.widget.ImageView)3 ActionMenuChildView (android.support.v7.internal.view.menu.ActionMenuView.ActionMenuChildView)2 SubMenuBuilder (android.support.v7.view.menu.SubMenuBuilder)2 MenuItem (android.view.MenuItem)2 ViewGroup (android.view.ViewGroup)2 Animator (android.animation.Animator)1 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)1 AnimatorSet (android.animation.AnimatorSet)1 ObjectAnimator (android.animation.ObjectAnimator)1 Resources (android.content.res.Resources)1 MenuBuilder (android.support.v7.view.menu.MenuBuilder)1 SparseBooleanArray (android.util.SparseBooleanArray)1 AccelerateInterpolator (android.view.animation.AccelerateInterpolator)1 TextView (android.widget.TextView)1 MenuPopupHelper (com.arlib.floatingsearchview.util.MenuPopupHelper)1 ViewPropertyObjectAnimator (com.bartoszlipinski.viewpropertyobjectanimator.ViewPropertyObjectAnimator)1 ArrayList (java.util.ArrayList)1 Description (org.hamcrest.Description)1