NavigationMenuItemView.java revision 66698bb15ba0f873aa1c2290cc50d6bb839a474a
1/* 2 * Copyright (C) 2015 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 android.content.Context; 20import android.content.res.ColorStateList; 21import android.graphics.Color; 22import android.graphics.drawable.ColorDrawable; 23import android.graphics.drawable.Drawable; 24import android.graphics.drawable.StateListDrawable; 25import android.support.design.R; 26import android.support.v4.graphics.drawable.DrawableCompat; 27import android.support.v4.widget.TextViewCompat; 28import android.support.v7.view.menu.MenuItemImpl; 29import android.support.v7.view.menu.MenuView; 30import android.util.AttributeSet; 31import android.util.TypedValue; 32import android.view.LayoutInflater; 33import android.view.View; 34import android.view.ViewStub; 35import android.widget.CheckedTextView; 36import android.widget.FrameLayout; 37 38/** 39 * @hide 40 */ 41public class NavigationMenuItemView extends ForegroundLinearLayout implements MenuView.ItemView { 42 43 private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked}; 44 45 private final int mIconSize; 46 47 private final CheckedTextView mTextView; 48 49 private FrameLayout mActionArea; 50 51 private MenuItemImpl mItemData; 52 53 private ColorStateList mIconTintList; 54 55 public NavigationMenuItemView(Context context) { 56 this(context, null); 57 } 58 59 public NavigationMenuItemView(Context context, AttributeSet attrs) { 60 this(context, attrs, 0); 61 } 62 63 public NavigationMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) { 64 super(context, attrs, defStyleAttr); 65 setOrientation(HORIZONTAL); 66 LayoutInflater.from(context).inflate(R.layout.design_navigation_menu_item, this, true); 67 mIconSize = context.getResources().getDimensionPixelSize( 68 R.dimen.design_navigation_icon_size); 69 mTextView = (CheckedTextView) findViewById(R.id.design_menu_item_text); 70 mTextView.setDuplicateParentStateEnabled(true); 71 } 72 73 @Override 74 public void initialize(MenuItemImpl itemData, int menuType) { 75 mItemData = itemData; 76 77 setVisibility(itemData.isVisible() ? VISIBLE : GONE); 78 79 if (getBackground() == null) { 80 setBackgroundDrawable(createDefaultBackground()); 81 } 82 83 setCheckable(itemData.isCheckable()); 84 setChecked(itemData.isChecked()); 85 setEnabled(itemData.isEnabled()); 86 setTitle(itemData.getTitle()); 87 setIcon(itemData.getIcon()); 88 setActionView(itemData.getActionView()); 89 } 90 91 public void recycle() { 92 if (mActionArea != null) { 93 mActionArea.removeAllViews(); 94 } 95 mTextView.setCompoundDrawables(null, null, null, null); 96 } 97 98 private void setActionView(View actionView) { 99 if (mActionArea == null) { 100 mActionArea = (FrameLayout) ((ViewStub) findViewById( 101 R.id.design_menu_item_action_area_stub)).inflate(); 102 } 103 mActionArea.removeAllViews(); 104 if (actionView != null) { 105 mActionArea.addView(actionView); 106 } 107 } 108 109 private StateListDrawable createDefaultBackground() { 110 TypedValue value = new TypedValue(); 111 if (getContext().getTheme().resolveAttribute(R.attr.colorControlHighlight, value, true)) { 112 StateListDrawable drawable = new StateListDrawable(); 113 drawable.addState(CHECKED_STATE_SET, new ColorDrawable(value.data)); 114 drawable.addState(EMPTY_STATE_SET, new ColorDrawable(Color.TRANSPARENT)); 115 return drawable; 116 } 117 return null; 118 } 119 120 @Override 121 public MenuItemImpl getItemData() { 122 return mItemData; 123 } 124 125 @Override 126 public void setTitle(CharSequence title) { 127 mTextView.setText(title); 128 } 129 130 @Override 131 public void setCheckable(boolean checkable) { 132 refreshDrawableState(); 133 } 134 135 @Override 136 public void setChecked(boolean checked) { 137 refreshDrawableState(); 138 mTextView.setChecked(checked); 139 } 140 141 @Override 142 public void setShortcut(boolean showShortcut, char shortcutKey) { 143 } 144 145 @Override 146 public void setIcon(Drawable icon) { 147 if (icon != null) { 148 Drawable.ConstantState state = icon.getConstantState(); 149 icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate(); 150 icon.setBounds(0, 0, mIconSize, mIconSize); 151 DrawableCompat.setTintList(icon, mIconTintList); 152 } 153 TextViewCompat.setCompoundDrawablesRelative(mTextView, icon, null, null, null); 154 } 155 156 @Override 157 public boolean prefersCondensedTitle() { 158 return false; 159 } 160 161 @Override 162 public boolean showsIcon() { 163 return true; 164 } 165 166 @Override 167 protected int[] onCreateDrawableState(int extraSpace) { 168 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 169 if (mItemData != null && mItemData.isCheckable() && mItemData.isChecked()) { 170 mergeDrawableStates(drawableState, CHECKED_STATE_SET); 171 } 172 return drawableState; 173 } 174 175 void setIconTintList(ColorStateList tintList) { 176 mIconTintList = tintList; 177 if (mItemData != null) { 178 // Update the icon so that the tint takes effect 179 setIcon(mItemData.getIcon()); 180 } 181 } 182 183 public void setTextAppearance(Context context, int textAppearance) { 184 mTextView.setTextAppearance(context, textAppearance); 185 } 186 187 public void setTextColor(ColorStateList colors) { 188 mTextView.setTextColor(colors); 189 } 190 191} 192