2021-06-27 20:44:35 +00:00
|
|
|
package androidx.appcompat.view.menu;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AbsListView;
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.RadioButton;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.RestrictTo;
|
|
|
|
import androidx.appcompat.R;
|
|
|
|
import androidx.appcompat.view.menu.MenuView;
|
|
|
|
import androidx.appcompat.widget.TintTypedArray;
|
|
|
|
import androidx.core.view.ViewCompat;
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
|
|
public class ListMenuItemView extends LinearLayout implements MenuView.ItemView, AbsListView.SelectionBoundsAdjuster {
|
|
|
|
private static final String TAG = "ListMenuItemView";
|
|
|
|
private Drawable mBackground;
|
|
|
|
private CheckBox mCheckBox;
|
|
|
|
private LinearLayout mContent;
|
|
|
|
private boolean mForceShowIcon;
|
|
|
|
private ImageView mGroupDivider;
|
|
|
|
private boolean mHasListDivider;
|
|
|
|
private ImageView mIconView;
|
|
|
|
private LayoutInflater mInflater;
|
|
|
|
private MenuItemImpl mItemData;
|
|
|
|
private boolean mPreserveIconSpacing;
|
|
|
|
private RadioButton mRadioButton;
|
|
|
|
private TextView mShortcutView;
|
|
|
|
private Drawable mSubMenuArrow;
|
|
|
|
private ImageView mSubMenuArrowView;
|
|
|
|
private int mTextAppearance;
|
|
|
|
private Context mTextAppearanceContext;
|
|
|
|
private TextView mTitleView;
|
|
|
|
|
|
|
|
public ListMenuItemView(Context context, AttributeSet attributeSet) {
|
|
|
|
this(context, attributeSet, R.attr.listMenuViewStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListMenuItemView(Context context, AttributeSet attributeSet, int i) {
|
|
|
|
super(context, attributeSet);
|
|
|
|
TintTypedArray obtainStyledAttributes = TintTypedArray.obtainStyledAttributes(getContext(), attributeSet, R.styleable.MenuView, i, 0);
|
|
|
|
this.mBackground = obtainStyledAttributes.getDrawable(R.styleable.MenuView_android_itemBackground);
|
|
|
|
this.mTextAppearance = obtainStyledAttributes.getResourceId(R.styleable.MenuView_android_itemTextAppearance, -1);
|
|
|
|
this.mPreserveIconSpacing = obtainStyledAttributes.getBoolean(R.styleable.MenuView_preserveIconSpacing, false);
|
|
|
|
this.mTextAppearanceContext = context;
|
|
|
|
this.mSubMenuArrow = obtainStyledAttributes.getDrawable(R.styleable.MenuView_subMenuArrow);
|
|
|
|
TypedArray obtainStyledAttributes2 = context.getTheme().obtainStyledAttributes(null, new int[]{16843049}, R.attr.dropDownListViewStyle, 0);
|
|
|
|
this.mHasListDivider = obtainStyledAttributes2.hasValue(0);
|
|
|
|
obtainStyledAttributes.recycle();
|
|
|
|
obtainStyledAttributes2.recycle();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addContentView(View view) {
|
|
|
|
addContentView(view, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addContentView(View view, int i) {
|
|
|
|
LinearLayout linearLayout = this.mContent;
|
|
|
|
if (linearLayout != null) {
|
|
|
|
linearLayout.addView(view, i);
|
|
|
|
} else {
|
|
|
|
addView(view, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private LayoutInflater getInflater() {
|
|
|
|
if (this.mInflater == null) {
|
|
|
|
this.mInflater = LayoutInflater.from(getContext());
|
|
|
|
}
|
|
|
|
return this.mInflater;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void insertCheckBox() {
|
|
|
|
CheckBox checkBox = (CheckBox) getInflater().inflate(R.layout.abc_list_menu_item_checkbox, (ViewGroup) this, false);
|
|
|
|
this.mCheckBox = checkBox;
|
|
|
|
addContentView(checkBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void insertIconView() {
|
|
|
|
ImageView imageView = (ImageView) getInflater().inflate(R.layout.abc_list_menu_item_icon, (ViewGroup) this, false);
|
|
|
|
this.mIconView = imageView;
|
|
|
|
addContentView(imageView, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void insertRadioButton() {
|
|
|
|
RadioButton radioButton = (RadioButton) getInflater().inflate(R.layout.abc_list_menu_item_radio, (ViewGroup) this, false);
|
|
|
|
this.mRadioButton = radioButton;
|
|
|
|
addContentView(radioButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setSubMenuArrowVisible(boolean z2) {
|
|
|
|
ImageView imageView = this.mSubMenuArrowView;
|
|
|
|
if (imageView != null) {
|
|
|
|
imageView.setVisibility(z2 ? 0 : 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // android.widget.AbsListView.SelectionBoundsAdjuster
|
|
|
|
public void adjustListItemSelectionBounds(Rect rect) {
|
|
|
|
ImageView imageView = this.mGroupDivider;
|
|
|
|
if (imageView != null && imageView.getVisibility() == 0) {
|
|
|
|
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) this.mGroupDivider.getLayoutParams();
|
|
|
|
rect.top = this.mGroupDivider.getHeight() + layoutParams.topMargin + layoutParams.bottomMargin + rect.top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
|
|
|
public MenuItemImpl getItemData() {
|
|
|
|
return this.mItemData;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
|
|
|
public void initialize(MenuItemImpl menuItemImpl, int i) {
|
|
|
|
this.mItemData = menuItemImpl;
|
|
|
|
setVisibility(menuItemImpl.isVisible() ? 0 : 8);
|
|
|
|
setTitle(menuItemImpl.getTitleForItemView(this));
|
|
|
|
setCheckable(menuItemImpl.isCheckable());
|
|
|
|
setShortcut(menuItemImpl.shouldShowShortcut(), menuItemImpl.getShortcut());
|
|
|
|
setIcon(menuItemImpl.getIcon());
|
|
|
|
setEnabled(menuItemImpl.isEnabled());
|
|
|
|
setSubMenuArrowVisible(menuItemImpl.hasSubMenu());
|
|
|
|
setContentDescription(menuItemImpl.getContentDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // android.view.View
|
|
|
|
public void onFinishInflate() {
|
|
|
|
super.onFinishInflate();
|
|
|
|
ViewCompat.setBackground(this, this.mBackground);
|
|
|
|
TextView textView = (TextView) findViewById(R.id.title);
|
|
|
|
this.mTitleView = textView;
|
|
|
|
int i = this.mTextAppearance;
|
|
|
|
if (i != -1) {
|
|
|
|
textView.setTextAppearance(this.mTextAppearanceContext, i);
|
|
|
|
}
|
|
|
|
this.mShortcutView = (TextView) findViewById(R.id.shortcut);
|
|
|
|
ImageView imageView = (ImageView) findViewById(R.id.submenuarrow);
|
|
|
|
this.mSubMenuArrowView = imageView;
|
|
|
|
if (imageView != null) {
|
|
|
|
imageView.setImageDrawable(this.mSubMenuArrow);
|
|
|
|
}
|
|
|
|
this.mGroupDivider = (ImageView) findViewById(R.id.group_divider);
|
|
|
|
this.mContent = (LinearLayout) findViewById(R.id.content);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // android.widget.LinearLayout, android.view.View
|
|
|
|
public void onMeasure(int i, int i2) {
|
|
|
|
if (this.mIconView != null && this.mPreserveIconSpacing) {
|
|
|
|
ViewGroup.LayoutParams layoutParams = getLayoutParams();
|
|
|
|
LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) this.mIconView.getLayoutParams();
|
|
|
|
int i3 = layoutParams.height;
|
|
|
|
if (i3 > 0 && layoutParams2.width <= 0) {
|
|
|
|
layoutParams2.width = i3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.onMeasure(i, i2);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
|
|
|
public boolean prefersCondensedTitle() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
|
|
|
public void setCheckable(boolean z2) {
|
|
|
|
CompoundButton compoundButton;
|
|
|
|
CompoundButton compoundButton2;
|
|
|
|
if (z2 || this.mRadioButton != null || this.mCheckBox != null) {
|
|
|
|
if (this.mItemData.isExclusiveCheckable()) {
|
|
|
|
if (this.mRadioButton == null) {
|
|
|
|
insertRadioButton();
|
|
|
|
}
|
|
|
|
compoundButton2 = this.mRadioButton;
|
|
|
|
compoundButton = this.mCheckBox;
|
|
|
|
} else {
|
|
|
|
if (this.mCheckBox == null) {
|
|
|
|
insertCheckBox();
|
|
|
|
}
|
|
|
|
compoundButton2 = this.mCheckBox;
|
|
|
|
compoundButton = this.mRadioButton;
|
|
|
|
}
|
|
|
|
if (z2) {
|
|
|
|
compoundButton2.setChecked(this.mItemData.isChecked());
|
|
|
|
if (compoundButton2.getVisibility() != 0) {
|
|
|
|
compoundButton2.setVisibility(0);
|
|
|
|
}
|
|
|
|
if (compoundButton != null && compoundButton.getVisibility() != 8) {
|
|
|
|
compoundButton.setVisibility(8);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CheckBox checkBox = this.mCheckBox;
|
|
|
|
if (checkBox != null) {
|
|
|
|
checkBox.setVisibility(8);
|
|
|
|
}
|
|
|
|
RadioButton radioButton = this.mRadioButton;
|
|
|
|
if (radioButton != null) {
|
|
|
|
radioButton.setVisibility(8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
|
|
|
public void setChecked(boolean z2) {
|
|
|
|
CompoundButton compoundButton;
|
|
|
|
if (this.mItemData.isExclusiveCheckable()) {
|
|
|
|
if (this.mRadioButton == null) {
|
|
|
|
insertRadioButton();
|
|
|
|
}
|
|
|
|
compoundButton = this.mRadioButton;
|
|
|
|
} else {
|
|
|
|
if (this.mCheckBox == null) {
|
|
|
|
insertCheckBox();
|
|
|
|
}
|
|
|
|
compoundButton = this.mCheckBox;
|
|
|
|
}
|
|
|
|
compoundButton.setChecked(z2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setForceShowIcon(boolean z2) {
|
|
|
|
this.mForceShowIcon = z2;
|
|
|
|
this.mPreserveIconSpacing = z2;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setGroupDividerEnabled(boolean z2) {
|
|
|
|
ImageView imageView = this.mGroupDivider;
|
|
|
|
if (imageView != null) {
|
|
|
|
imageView.setVisibility((this.mHasListDivider || !z2) ? 8 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
|
|
|
public void setIcon(Drawable drawable) {
|
|
|
|
boolean z2 = this.mItemData.shouldShowIcon() || this.mForceShowIcon;
|
|
|
|
if (z2 || this.mPreserveIconSpacing) {
|
|
|
|
ImageView imageView = this.mIconView;
|
|
|
|
if (imageView != null || drawable != null || this.mPreserveIconSpacing) {
|
|
|
|
if (imageView == null) {
|
|
|
|
insertIconView();
|
|
|
|
}
|
|
|
|
if (drawable != null || this.mPreserveIconSpacing) {
|
|
|
|
ImageView imageView2 = this.mIconView;
|
|
|
|
if (!z2) {
|
|
|
|
drawable = null;
|
|
|
|
}
|
|
|
|
imageView2.setImageDrawable(drawable);
|
|
|
|
if (this.mIconView.getVisibility() != 0) {
|
|
|
|
this.mIconView.setVisibility(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.mIconView.setVisibility(8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
2021-12-21 23:37:30 +00:00
|
|
|
public void setShortcut(boolean z2, char c) {
|
2021-06-27 20:44:35 +00:00
|
|
|
int i = (!z2 || !this.mItemData.shouldShowShortcut()) ? 8 : 0;
|
|
|
|
if (i == 0) {
|
|
|
|
this.mShortcutView.setText(this.mItemData.getShortcutLabel());
|
|
|
|
}
|
|
|
|
if (this.mShortcutView.getVisibility() != i) {
|
|
|
|
this.mShortcutView.setVisibility(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
|
|
|
public void setTitle(CharSequence charSequence) {
|
|
|
|
if (charSequence != null) {
|
|
|
|
this.mTitleView.setText(charSequence);
|
|
|
|
if (this.mTitleView.getVisibility() != 0) {
|
|
|
|
this.mTitleView.setVisibility(0);
|
|
|
|
}
|
|
|
|
} else if (this.mTitleView.getVisibility() != 8) {
|
|
|
|
this.mTitleView.setVisibility(8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuView.ItemView
|
|
|
|
public boolean showsIcon() {
|
|
|
|
return this.mForceShowIcon;
|
|
|
|
}
|
|
|
|
}
|