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