NavigationMenuItemView.java revision d3ee2c7d22c0cb204ea9077fa9e64511266d6799
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.internal.view.menu.MenuItemImpl; 29import android.support.v7.internal.view.menu.MenuView; 30import android.util.AttributeSet; 31import android.util.TypedValue; 32import android.view.View; 33import android.widget.TextView; 34 35/** 36 * @hide 37 */ 38public class NavigationMenuItemView extends TextView implements MenuView.ItemView { 39 40 private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked}; 41 42 private static final int[] DISABLED_STATE_SET = {-android.R.attr.state_enabled}; 43 44 private int mIconSize; 45 46 private MenuItemImpl mItemData; 47 48 private ColorStateList mTintList; 49 50 public NavigationMenuItemView(Context context) { 51 this(context, null); 52 } 53 54 public NavigationMenuItemView(Context context, AttributeSet attrs) { 55 this(context, attrs, 0); 56 } 57 58 public NavigationMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) { 59 super(context, attrs, defStyleAttr); 60 mIconSize = context.getResources().getDimensionPixelSize(R.dimen.drawer_icon_size); 61 } 62 63 @Override 64 public void initialize(MenuItemImpl itemData, int menuType) { 65 mItemData = itemData; 66 67 setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); 68 69 if (mTintList == null && (itemData.isChecked() || !itemData.isEnabled())) { 70 mTintList = createDefaultTintList(); 71 setTextColor(mTintList); 72 } 73 if (getBackground() == null) { 74 setBackgroundDrawable(createDefaultBackground()); 75 } 76 77 setCheckable(itemData.isCheckable()); 78 setChecked(itemData.isChecked()); 79 setTitle(itemData.getTitle()); 80 setIcon(itemData.getIcon()); 81 setEnabled(itemData.isEnabled()); 82 } 83 84 private ColorStateList createDefaultTintList() { 85 TypedValue value = new TypedValue(); 86 if (!getContext().getTheme() 87 .resolveAttribute(android.R.attr.textColorPrimary, value, true)) { 88 return null; 89 } 90 ColorStateList base = getResources().getColorStateList(value.resourceId); 91 if (!getContext().getTheme().resolveAttribute(R.attr.colorPrimary, value, true)) { 92 return null; 93 } 94 int colorPrimary = value.data; 95 int defaultColor = base.getDefaultColor(); 96 return new ColorStateList(new int[][]{ 97 DISABLED_STATE_SET, 98 CHECKED_STATE_SET, 99 EMPTY_STATE_SET 100 }, new int[]{ 101 base.getColorForState(DISABLED_STATE_SET, defaultColor), 102 colorPrimary, 103 defaultColor 104 }); 105 } 106 107 private StateListDrawable createDefaultBackground() { 108 TypedValue value = new TypedValue(); 109 if (getContext().getTheme() 110 .resolveAttribute(R.attr.colorControlHighlight, value, true)) { 111 StateListDrawable drawable = new StateListDrawable(); 112 drawable.addState(CHECKED_STATE_SET, new ColorDrawable(value.data)); 113 drawable.addState(EMPTY_STATE_SET, new ColorDrawable(Color.TRANSPARENT)); 114 return drawable; 115 } 116 return null; 117 } 118 119 @Override 120 public MenuItemImpl getItemData() { 121 return mItemData; 122 } 123 124 @Override 125 public void setTitle(CharSequence title) { 126 setText(title); 127 } 128 129 @Override 130 public void setCheckable(boolean checkable) { 131 if (checkable && mTintList != null) { 132 setTextColor(mTintList); 133 } 134 } 135 136 @Override 137 public void setChecked(boolean checked) { 138 refreshDrawableState(); 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 icon = DrawableCompat.wrap(icon); 149 icon.setBounds(0, 0, mIconSize, mIconSize); 150 icon = icon.mutate(); 151 if (mItemData.isChecked() || !mItemData.isEnabled()) { 152 DrawableCompat.setTintList(icon, mTintList); 153 } else { 154 DrawableCompat.setTintList(icon, null); 155 } 156 } 157 TextViewCompat.setCompoundDrawablesRelative(this, icon, null, null, null); 158 } 159 160 @Override 161 public boolean prefersCondensedTitle() { 162 return false; 163 } 164 165 @Override 166 public boolean showsIcon() { 167 return true; 168 } 169 170 @Override 171 protected int[] onCreateDrawableState(int extraSpace) { 172 if (mItemData != null && mItemData.isChecked()) { 173 return mergeDrawableStates(super.onCreateDrawableState(extraSpace + 1), 174 CHECKED_STATE_SET); 175 } else { 176 return super.onCreateDrawableState(extraSpace); 177 } 178 } 179 180 public void setTintList(ColorStateList tintList) { 181 mTintList = tintList; 182 if (tintList != null) { 183 setTextColor(tintList); 184 } 185 } 186 187} 188