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