BottomNavigationMenuView.java revision e1e8ec0b26b8408a4da17729a8df33d0f04f0ccc
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 * Sets 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 * Sets 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 * 231 * @param background the resource ID of the background 232 */ 233 public void setItemBackgroundRes(int background) { 234 mItemBackgroundRes = background; 235 if (mButtons == null) return; 236 for (BottomNavigationItemView item : mButtons) { 237 item.setItemBackground(background); 238 } 239 } 240 241 /** 242 * Returns the resource ID for the background of the menu items. 243 * 244 * @return the resource ID for the background 245 */ 246 public int getItemBackgroundRes() { 247 return mItemBackgroundRes; 248 } 249 250 public void setPresenter(BottomNavigationPresenter presenter) { 251 mPresenter = presenter; 252 } 253 254 public void buildMenuView() { 255 if (mButtons != null) { 256 for (BottomNavigationItemView item : mButtons) { 257 sItemPool.release(item); 258 } 259 } 260 removeAllViews(); 261 mButtons = new BottomNavigationItemView[mMenu.size()]; 262 mShiftingMode = mMenu.size() > 3; 263 for (int i = 0; i < mMenu.size(); i++) { 264 mPresenter.setUpdateSuspended(true); 265 mMenu.getItem(i).setCheckable(true); 266 mPresenter.setUpdateSuspended(false); 267 BottomNavigationItemView child = getNewItem(); 268 mButtons[i] = child; 269 child.setIconTintList(mItemIconTint); 270 child.setTextColor(mItemTextColor); 271 child.setItemBackground(mItemBackgroundRes); 272 child.setShiftingMode(mShiftingMode); 273 child.initialize((MenuItemImpl) mMenu.getItem(i), 0); 274 child.setItemPosition(i); 275 child.setOnClickListener(mOnClickListener); 276 addView(child); 277 } 278 } 279 280 public void updateMenuView() { 281 final int menuSize = mMenu.size(); 282 if (menuSize != mButtons.length) { 283 // The size has changed. Rebuild menu view from scratch. 284 buildMenuView(); 285 return; 286 } 287 for (int i = 0; i < menuSize; i++) { 288 mPresenter.setUpdateSuspended(true); 289 mButtons[i].initialize((MenuItemImpl) mMenu.getItem(i), 0); 290 mPresenter.setUpdateSuspended(false); 291 } 292 } 293 294 private void activateNewButton(int newButton) { 295 if (mActiveButton == newButton) return; 296 297 mAnimationHelper.beginDelayedTransition(this); 298 299 mPresenter.setUpdateSuspended(true); 300 mButtons[mActiveButton].setChecked(false); 301 mButtons[newButton].setChecked(true); 302 mPresenter.setUpdateSuspended(false); 303 304 mActiveButton = newButton; 305 } 306 307 private BottomNavigationItemView getNewItem() { 308 BottomNavigationItemView item = sItemPool.acquire(); 309 if (item == null) { 310 item = new BottomNavigationItemView(getContext()); 311 } 312 return item; 313 } 314} 315