ListMenuItemView.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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 mShortcutView.setText(mItemData.getShortcutLabel()); 175 176 final int newVisibility = showShortcut ? VISIBLE : GONE; 177 if (mShortcutView.getVisibility() != newVisibility) { 178 mShortcutView.setVisibility(newVisibility); 179 } 180 } 181 182 public void setIcon(Drawable icon) { 183 184 if (!mItemData.shouldShowIcon(mMenuType)) { 185 return; 186 } 187 188 if (mIconView == null && icon == null) { 189 return; 190 } 191 192 if (mIconView == null) { 193 insertIconView(); 194 } 195 196 if (icon != null) { 197 mIconView.setImageDrawable(icon); 198 199 if (mIconView.getVisibility() != VISIBLE) { 200 mIconView.setVisibility(VISIBLE); 201 } 202 } else { 203 mIconView.setVisibility(GONE); 204 } 205 } 206 207 private void insertIconView() { 208 LayoutInflater inflater = mItemData.getLayoutInflater(mMenuType); 209 mIconView = (ImageView) inflater.inflate(com.android.internal.R.layout.list_menu_item_icon, 210 this, false); 211 addView(mIconView, 0); 212 } 213 214 private void insertRadioButton() { 215 LayoutInflater inflater = mItemData.getLayoutInflater(mMenuType); 216 mRadioButton = 217 (RadioButton) inflater.inflate(com.android.internal.R.layout.list_menu_item_radio, 218 this, false); 219 addView(mRadioButton, 0); 220 } 221 222 private void insertCheckBox() { 223 LayoutInflater inflater = mItemData.getLayoutInflater(mMenuType); 224 mCheckBox = 225 (CheckBox) inflater.inflate(com.android.internal.R.layout.list_menu_item_checkbox, 226 this, false); 227 addView(mCheckBox, 0); 228 } 229 230 public boolean prefersCondensedTitle() { 231 return false; 232 } 233 234 public boolean showsIcon() { 235 return false; 236 } 237 238} 239