BottomNavigationMenuView.java revision 578015fc3e5ddc0fdb95db7ad0d00822c54be6df
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.design.internal;
18
19import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
20
21import android.content.Context;
22import android.content.res.ColorStateList;
23import android.content.res.Resources;
24import android.os.Build;
25import android.support.annotation.Nullable;
26import android.support.annotation.RestrictTo;
27import android.support.design.R;
28import android.support.v4.util.Pools;
29import android.support.v4.view.ViewCompat;
30import android.support.v7.view.menu.MenuBuilder;
31import android.support.v7.view.menu.MenuItemImpl;
32import android.support.v7.view.menu.MenuView;
33import android.util.AttributeSet;
34import android.view.View;
35import android.view.ViewGroup;
36
37/**
38 * @hide For internal use only.
39 */
40@RestrictTo(GROUP_ID)
41public class BottomNavigationMenuView extends ViewGroup implements MenuView {
42    private final int mInactiveItemMaxWidth;
43    private final int mInactiveItemMinWidth;
44    private final int mActiveItemMaxWidth;
45    private final int mItemHeight;
46    private final OnClickListener mOnClickListener;
47    private final BottomNavigationAnimationHelperBase mAnimationHelper;
48    private static final Pools.Pool<BottomNavigationItemView> sItemPool =
49            new Pools.SynchronizedPool<>(5);
50
51    private boolean mShiftingMode = true;
52
53    private BottomNavigationItemView[] mButtons;
54    private int mActiveButton = 0;
55    private ColorStateList mItemIconTint;
56    private ColorStateList mItemTextColor;
57    private int mItemBackgroundRes;
58    private int[] mTempChildWidths;
59
60    private BottomNavigationPresenter mPresenter;
61    private MenuBuilder mMenu;
62
63    public BottomNavigationMenuView(Context context) {
64        this(context, null);
65    }
66
67    public BottomNavigationMenuView(Context context, AttributeSet attrs) {
68        super(context, attrs);
69        final Resources res = getResources();
70        mInactiveItemMaxWidth = res.getDimensionPixelSize(
71                R.dimen.design_bottom_navigation_item_max_width);
72        mInactiveItemMinWidth = res.getDimensionPixelSize(
73                R.dimen.design_bottom_navigation_item_min_width);
74        mActiveItemMaxWidth = res.getDimensionPixelSize(
75                R.dimen.design_bottom_navigation_active_item_max_width);
76        mItemHeight = res.getDimensionPixelSize(R.dimen.design_bottom_navigation_height);
77
78        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
79            mAnimationHelper = new BottomNavigationAnimationHelperIcs();
80        } else {
81            mAnimationHelper = new BottomNavigationAnimationHelperBase();
82        }
83
84        mOnClickListener = new OnClickListener() {
85            @Override
86            public void onClick(View v) {
87                final BottomNavigationItemView itemView = (BottomNavigationItemView) v;
88                final int itemPosition = itemView.getItemPosition();
89                if (!mMenu.performItemAction(itemView.getItemData(), mPresenter, 0)) {
90                    activateNewButton(itemPosition);
91                }
92            }
93        };
94        mTempChildWidths = new int[BottomNavigationMenu.MAX_ITEM_COUNT];
95    }
96
97    @Override
98    public void initialize(MenuBuilder menu) {
99        mMenu = menu;
100        if (mMenu == null) return;
101        if (mMenu.size() > mActiveButton) {
102            mMenu.getItem(mActiveButton).setChecked(true);
103        }
104    }
105
106    @Override
107    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
108        final int width = MeasureSpec.getSize(widthMeasureSpec);
109        final int count = getChildCount();
110
111        final int heightSpec = MeasureSpec.makeMeasureSpec(mItemHeight, MeasureSpec.EXACTLY);
112
113        if (mShiftingMode) {
114            final int inactiveCount = count - 1;
115            final int activeMaxAvailable = width - inactiveCount * mInactiveItemMinWidth;
116            final int activeWidth = Math.min(activeMaxAvailable, mActiveItemMaxWidth);
117            final int inactiveMaxAvailable = (width - activeWidth) / inactiveCount;
118            final int inactiveWidth = Math.min(inactiveMaxAvailable, mInactiveItemMaxWidth);
119            int extra = width - activeWidth - inactiveWidth * inactiveCount;
120            for (int i = 0; i < count; i++) {
121                mTempChildWidths[i] = (i == mActiveButton) ? activeWidth : inactiveWidth;
122                if (extra > 0) {
123                    mTempChildWidths[i]++;
124                    extra--;
125                }
126            }
127        } else {
128            final int maxAvailable = width / count;
129            final int childWidth = Math.min(maxAvailable, mActiveItemMaxWidth);
130            int extra = width - childWidth * count;
131            for (int i = 0; i < count; i++) {
132                mTempChildWidths[i] = childWidth;
133                if (extra > 0) {
134                    mTempChildWidths[i]++;
135                    extra--;
136                }
137            }
138        }
139
140        int totalWidth = 0;
141        for (int i = 0; i < count; i++) {
142            final View child = getChildAt(i);
143            if (child.getVisibility() == GONE) {
144                continue;
145            }
146            child.measure(MeasureSpec.makeMeasureSpec(mTempChildWidths[i], MeasureSpec.EXACTLY),
147                    heightSpec);
148            ViewGroup.LayoutParams params = child.getLayoutParams();
149            params.width = child.getMeasuredWidth();
150            totalWidth += child.getMeasuredWidth();
151        }
152        setMeasuredDimension(
153                ViewCompat.resolveSizeAndState(totalWidth,
154                        MeasureSpec.makeMeasureSpec(totalWidth, MeasureSpec.EXACTLY), 0),
155                ViewCompat.resolveSizeAndState(mItemHeight, heightSpec, 0));
156    }
157
158    @Override
159    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
160        final int count = getChildCount();
161        final int width = right - left;
162        final int height = bottom - top;
163        int used = 0;
164        for (int i = 0; i < count; i++) {
165            final View child = getChildAt(i);
166            if (child.getVisibility() == GONE) {
167                continue;
168            }
169            if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) {
170                child.layout(width - used - child.getMeasuredWidth(), 0, width - used, height);
171            } else {
172                child.layout(used, 0, child.getMeasuredWidth() + used, height);
173            }
174            used += child.getMeasuredWidth();
175        }
176    }
177
178    @Override
179    public int getWindowAnimations() {
180        return 0;
181    }
182
183    /**
184     * Set the tint which is applied to the menu items' icons.
185     *
186     * @param tint the tint to apply.
187     */
188    public void setIconTintList(ColorStateList tint) {
189        mItemIconTint = tint;
190        if (mButtons == null) return;
191        for (BottomNavigationItemView item : mButtons) {
192            item.setIconTintList(tint);
193        }
194    }
195
196    /**
197     * Returns the tint which is applied to menu items' icons.
198     *
199     * @return The ColorStateList that is used to tint menu items' icons.
200     */
201    @Nullable
202    public ColorStateList getIconTintList() {
203        return mItemIconTint;
204    }
205
206    /**
207     * Set the text color to be used on menu items.
208     *
209     * @param color the ColorStateList used for menu items' text.
210     */
211    public void setItemTextColor(ColorStateList color) {
212        mItemTextColor = color;
213        if (mButtons == null) return;
214        for (BottomNavigationItemView item : mButtons) {
215            item.setTextColor(color);
216        }
217    }
218
219    /**
220     * Returns the text color used on menu items.
221     *
222     * @return the ColorStateList used for menu items' text.
223     */
224    public ColorStateList getItemTextColor() {
225        return mItemTextColor;
226    }
227
228    /**
229     * Sets the resource id to be used for item background.
230     * @param background the resource id of the background.
231     */
232    public void setItemBackgroundRes(int background) {
233        mItemBackgroundRes = background;
234        if (mButtons == null) return;
235        for (BottomNavigationItemView item : mButtons) {
236            item.setItemBackground(background);
237        }
238    }
239
240    /**
241     * Returns the background resource of the menu items.
242     *
243     * @return the resource id of the background.
244     */
245    public int getItemBackgroundRes() {
246        return mItemBackgroundRes;
247    }
248
249    public void setPresenter(BottomNavigationPresenter presenter) {
250        mPresenter = presenter;
251    }
252
253    public void buildMenuView() {
254        if (mButtons != null) {
255            for (BottomNavigationItemView item : mButtons) {
256                sItemPool.release(item);
257            }
258        }
259        removeAllViews();
260        mButtons = new BottomNavigationItemView[mMenu.size()];
261        mShiftingMode = mMenu.size() > 3;
262        for (int i = 0; i < mMenu.size(); i++) {
263            mPresenter.setUpdateSuspended(true);
264            mMenu.getItem(i).setCheckable(true);
265            mPresenter.setUpdateSuspended(false);
266            BottomNavigationItemView child = getNewItem();
267            mButtons[i] = child;
268            child.setIconTintList(mItemIconTint);
269            child.setTextColor(mItemTextColor);
270            child.setItemBackground(mItemBackgroundRes);
271            child.setShiftingMode(mShiftingMode);
272            child.initialize((MenuItemImpl) mMenu.getItem(i), 0);
273            child.setItemPosition(i);
274            child.setOnClickListener(mOnClickListener);
275            addView(child);
276        }
277    }
278
279    public void updateMenuView() {
280        final int menuSize = mMenu.size();
281        if (menuSize != mButtons.length) {
282            // The size has changed. Rebuild menu view from scratch.
283            buildMenuView();
284            return;
285        }
286        for (int i = 0; i < menuSize; i++) {
287            mPresenter.setUpdateSuspended(true);
288            mButtons[i].initialize((MenuItemImpl) mMenu.getItem(i), 0);
289            mPresenter.setUpdateSuspended(false);
290        }
291    }
292
293    private void activateNewButton(int newButton) {
294        if (mActiveButton == newButton) return;
295
296        mAnimationHelper.beginDelayedTransition(this);
297
298        mPresenter.setUpdateSuspended(true);
299        mButtons[mActiveButton].setChecked(false);
300        mButtons[newButton].setChecked(true);
301        mPresenter.setUpdateSuspended(false);
302
303        mActiveButton = newButton;
304    }
305
306    private BottomNavigationItemView getNewItem() {
307        BottomNavigationItemView item = sItemPool.acquire();
308        if (item == null) {
309            item = new BottomNavigationItemView(getContext());
310        }
311        return item;
312    }
313}
314