Source code
package android.support.design.internal;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Drawable.ConstantState;
import android.graphics.drawable.StateListDrawable;
import android.support.design.R;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.internal.view.menu.MenuItemImpl;
import android.support.v7.internal.view.menu.MenuView.ItemView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.CheckedTextView;
import android.widget.FrameLayout;
public class NavigationMenuItemView extends ForegroundLinearLayout implements ItemView {
private static final int[] CHECKED_STATE_SET = new int[]{16842912};
private FrameLayout mActionArea;
private final int mIconSize;
private ColorStateList mIconTintList;
private MenuItemImpl mItemData;
private final CheckedTextView mTextView;
public NavigationMenuItemView(Context context) {
this(context, null);
}
public NavigationMenuItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NavigationMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(0);
LayoutInflater.from(context).inflate(R.layout.design_navigation_menu_item, this, true);
this.mIconSize = context.getResources().getDimensionPixelSize(R.dimen.design_navigation_icon_size);
this.mTextView = (CheckedTextView) findViewById(R.id.design_menu_item_text);
this.mTextView.setDuplicateParentStateEnabled(true);
}
public void initialize(MenuItemImpl itemData, int menuType) {
this.mItemData = itemData;
setVisibility(itemData.isVisible() ? 0 : 8);
if (getBackground() == null) {
setBackgroundDrawable(createDefaultBackground());
}
setCheckable(itemData.isCheckable());
setChecked(itemData.isChecked());
setEnabled(itemData.isEnabled());
setTitle(itemData.getTitle());
setIcon(itemData.getIcon());
setActionView(itemData.getActionView());
}
public void recycle() {
if (this.mActionArea != null) {
this.mActionArea.removeAllViews();
}
this.mTextView.setCompoundDrawables(null, null, null, null);
}
private void setActionView(View actionView) {
if (this.mActionArea == null) {
this.mActionArea = (FrameLayout) ((ViewStub) findViewById(R.id.design_menu_item_action_area_stub)).inflate();
}
this.mActionArea.removeAllViews();
if (actionView != null) {
this.mActionArea.addView(actionView);
}
}
private StateListDrawable createDefaultBackground() {
TypedValue value = new TypedValue();
if (!getContext().getTheme().resolveAttribute(R.attr.colorControlHighlight, value, true)) {
return null;
}
StateListDrawable drawable = new StateListDrawable();
drawable.addState(CHECKED_STATE_SET, new ColorDrawable(value.data));
drawable.addState(EMPTY_STATE_SET, new ColorDrawable(0));
return drawable;
}
public MenuItemImpl getItemData() {
return this.mItemData;
}
public void setTitle(CharSequence title) {
this.mTextView.setText(title);
}
public void setCheckable(boolean checkable) {
refreshDrawableState();
}
public void setChecked(boolean checked) {
refreshDrawableState();
this.mTextView.setChecked(checked);
}
public void setShortcut(boolean showShortcut, char shortcutKey) {
}
public void setIcon(Drawable icon) {
if (icon != null) {
ConstantState state = icon.getConstantState();
if (state != null) {
icon = state.newDrawable();
}
icon = DrawableCompat.wrap(icon).mutate();
icon.setBounds(0, 0, this.mIconSize, this.mIconSize);
DrawableCompat.setTintList(icon, this.mIconTintList);
}
TextViewCompat.setCompoundDrawablesRelative(this.mTextView, icon, null, null, null);
}
public boolean prefersCondensedTitle() {
return false;
}
public boolean showsIcon() {
return true;
}
protected int[] onCreateDrawableState(int extraSpace) {
int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (this.mItemData != null && this.mItemData.isCheckable() && this.mItemData.isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
void setIconTintList(ColorStateList tintList) {
this.mIconTintList = tintList;
if (this.mItemData != null) {
setIcon(this.mItemData.getIcon());
}
}
public void setTextAppearance(Context context, int textAppearance) {
this.mTextView.setTextAppearance(context, textAppearance);
}
public void setTextColor(ColorStateList colors) {
this.mTextView.setTextColor(colors);
}
}