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