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