1/* 2 * Copyright (C) 2006 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.v7.internal.view.menu; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.drawable.Drawable; 22import android.support.v7.appcompat.R; 23import android.util.AttributeSet; 24import android.view.LayoutInflater; 25import android.view.View; 26import android.view.ViewGroup; 27import android.widget.CheckBox; 28import android.widget.CompoundButton; 29import android.widget.ImageView; 30import android.widget.LinearLayout; 31import android.widget.RadioButton; 32import android.widget.TextView; 33 34/** 35 * The item view for each item in the ListView-based MenuViews. 36 * 37 * @hide 38 */ 39public class ListMenuItemView extends LinearLayout implements MenuView.ItemView { 40 41 private static final String TAG = "ListMenuItemView"; 42 private MenuItemImpl mItemData; 43 44 private ImageView mIconView; 45 private RadioButton mRadioButton; 46 private TextView mTitleView; 47 private CheckBox mCheckBox; 48 private TextView mShortcutView; 49 50 private Drawable mBackground; 51 private int mTextAppearance; 52 private Context mTextAppearanceContext; 53 private boolean mPreserveIconSpacing; 54 55 private int mMenuType; 56 57 private Context mContext; 58 private LayoutInflater mInflater; 59 60 private boolean mForceShowIcon; 61 62 public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) { 63 super(context, attrs); 64 mContext = context; 65 66 TypedArray a = 67 context.obtainStyledAttributes( 68 attrs, R.styleable.MenuView, defStyle, 0); 69 70 mBackground = a.getDrawable(R.styleable.MenuView_android_itemBackground); 71 mTextAppearance = a.getResourceId(R.styleable. 72 MenuView_android_itemTextAppearance, -1); 73 mPreserveIconSpacing = a.getBoolean( 74 R.styleable.MenuView_android_preserveIconSpacing, false); 75 mTextAppearanceContext = context; 76 77 a.recycle(); 78 } 79 80 public ListMenuItemView(Context context, AttributeSet attrs) { 81 this(context, attrs, 0); 82 } 83 84 @Override 85 protected void onFinishInflate() { 86 super.onFinishInflate(); 87 88 setBackgroundDrawable(mBackground); 89 90 mTitleView = (TextView) findViewById(R.id.title); 91 if (mTextAppearance != -1) { 92 mTitleView.setTextAppearance(mTextAppearanceContext, 93 mTextAppearance); 94 } 95 96 mShortcutView = (TextView) findViewById(R.id.shortcut); 97 } 98 99 public void initialize(MenuItemImpl itemData, int menuType) { 100 mItemData = itemData; 101 mMenuType = menuType; 102 103 setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); 104 105 setTitle(itemData.getTitleForItemView(this)); 106 setCheckable(itemData.isCheckable()); 107 setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut()); 108 setIcon(itemData.getIcon()); 109 setEnabled(itemData.isEnabled()); 110 } 111 112 public void setForceShowIcon(boolean forceShow) { 113 mPreserveIconSpacing = mForceShowIcon = forceShow; 114 } 115 116 public void setTitle(CharSequence title) { 117 if (title != null) { 118 mTitleView.setText(title); 119 120 if (mTitleView.getVisibility() != VISIBLE) { 121 mTitleView.setVisibility(VISIBLE); 122 } 123 } else { 124 if (mTitleView.getVisibility() != GONE) { 125 mTitleView.setVisibility(GONE); 126 } 127 } 128 } 129 130 public MenuItemImpl getItemData() { 131 return mItemData; 132 } 133 134 public void setCheckable(boolean checkable) { 135 if (!checkable && mRadioButton == null && mCheckBox == null) { 136 return; 137 } 138 139 // Depending on whether its exclusive check or not, the checkbox or 140 // radio button will be the one in use (and the other will be otherCompoundButton) 141 final CompoundButton compoundButton; 142 final CompoundButton otherCompoundButton; 143 144 if (mItemData.isExclusiveCheckable()) { 145 if (mRadioButton == null) { 146 insertRadioButton(); 147 } 148 compoundButton = mRadioButton; 149 otherCompoundButton = mCheckBox; 150 } else { 151 if (mCheckBox == null) { 152 insertCheckBox(); 153 } 154 compoundButton = mCheckBox; 155 otherCompoundButton = mRadioButton; 156 } 157 158 if (checkable) { 159 compoundButton.setChecked(mItemData.isChecked()); 160 161 final int newVisibility = checkable ? VISIBLE : GONE; 162 if (compoundButton.getVisibility() != newVisibility) { 163 compoundButton.setVisibility(newVisibility); 164 } 165 166 // Make sure the other compound button isn't visible 167 if (otherCompoundButton != null && otherCompoundButton.getVisibility() != GONE) { 168 otherCompoundButton.setVisibility(GONE); 169 } 170 } else { 171 if (mCheckBox != null) { 172 mCheckBox.setVisibility(GONE); 173 } 174 if (mRadioButton != null) { 175 mRadioButton.setVisibility(GONE); 176 } 177 } 178 } 179 180 public void setChecked(boolean checked) { 181 CompoundButton compoundButton; 182 183 if (mItemData.isExclusiveCheckable()) { 184 if (mRadioButton == null) { 185 insertRadioButton(); 186 } 187 compoundButton = mRadioButton; 188 } else { 189 if (mCheckBox == null) { 190 insertCheckBox(); 191 } 192 compoundButton = mCheckBox; 193 } 194 195 compoundButton.setChecked(checked); 196 } 197 198 public void setShortcut(boolean showShortcut, char shortcutKey) { 199 final int newVisibility = (showShortcut && mItemData.shouldShowShortcut()) 200 ? VISIBLE : GONE; 201 202 if (newVisibility == VISIBLE) { 203 mShortcutView.setText(mItemData.getShortcutLabel()); 204 } 205 206 if (mShortcutView.getVisibility() != newVisibility) { 207 mShortcutView.setVisibility(newVisibility); 208 } 209 } 210 211 public void setIcon(Drawable icon) { 212 final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon; 213 if (!showIcon && !mPreserveIconSpacing) { 214 return; 215 } 216 217 if (mIconView == null && icon == null && !mPreserveIconSpacing) { 218 return; 219 } 220 221 if (mIconView == null) { 222 insertIconView(); 223 } 224 225 if (icon != null || mPreserveIconSpacing) { 226 mIconView.setImageDrawable(showIcon ? icon : null); 227 228 if (mIconView.getVisibility() != VISIBLE) { 229 mIconView.setVisibility(VISIBLE); 230 } 231 } else { 232 mIconView.setVisibility(GONE); 233 } 234 } 235 236 @Override 237 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 238 if (mIconView != null && mPreserveIconSpacing) { 239 // Enforce minimum icon spacing 240 ViewGroup.LayoutParams lp = getLayoutParams(); 241 LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams(); 242 if (lp.height > 0 && iconLp.width <= 0) { 243 iconLp.width = lp.height; 244 } 245 } 246 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 247 } 248 249 private void insertIconView() { 250 LayoutInflater inflater = getInflater(); 251 mIconView = (ImageView) inflater.inflate(R.layout.abc_list_menu_item_icon, 252 this, false); 253 addView(mIconView, 0); 254 } 255 256 private void insertRadioButton() { 257 LayoutInflater inflater = getInflater(); 258 mRadioButton = 259 (RadioButton) inflater.inflate(R.layout.abc_list_menu_item_radio, 260 this, false); 261 addView(mRadioButton); 262 } 263 264 private void insertCheckBox() { 265 LayoutInflater inflater = getInflater(); 266 mCheckBox = 267 (CheckBox) inflater.inflate(R.layout.abc_list_menu_item_checkbox, 268 this, false); 269 addView(mCheckBox); 270 } 271 272 public boolean prefersCondensedTitle() { 273 return false; 274 } 275 276 public boolean showsIcon() { 277 return mForceShowIcon; 278 } 279 280 private LayoutInflater getInflater() { 281 if (mInflater == null) { 282 mInflater = LayoutInflater.from(mContext); 283 } 284 return mInflater; 285 } 286} 287 288