2018-10-15
2018-10-19 本文已影响0人
graychen
引用
/**
- Constructs a new spinner with the given context, the supplied attribute
- set, default styles, popup mode (one of {@link #MODE_DIALOG} or
- {@link #MODE_DROPDOWN}), and the theme against which the popup should be
- inflated.
- @param context The context against which the view is inflated, which
provides access to the current theme, resources, etc.
- @param attrs The attributes of the XML tag that is inflating the view.
- @param defStyleAttr An attribute in the current theme that contains a
reference to a style resource that supplies default
values for the view. Can be 0 to not look for
defaults.
- @param defStyleRes A resource identifier of a style resource that
supplies default values for the view, used only if
defStyleAttr is 0 or can not be found in the theme.
Can be 0 to not look for defaults. * @param mode Constant describing how the user will select choices from
the spinner.
- @param popupTheme The theme against which the dialog or dropdown popup
should be inflated. May be {@code null} to use the
view theme. If set, this will override any value
specified by
{@link android.R.styleable#Spinner_popupTheme}.
- @see #MODE_DIALOG // SpinnerMode="dialog"
- @see #MODE_DROPDOWN // SpinnerMode="dropDown"
*/
public Spinner(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int mode,
Theme popupTheme) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.Spinner, defStyleAttr, defStyleRes);
if (popupTheme != null) {
mPopupContext = new ContextThemeWrapper(context, popupTheme);
} else {
final int popupThemeResId = a.getResourceId(R.styleable.Spinner_popupTheme, 0);
if (popupThemeResId != 0) {
mPopupContext = new ContextThemeWrapper(context, popupThemeResId);
} else {
mPopupContext = context;
}
}
if (mode == MODE_THEME) {
mode = a.getInt(R.styleable.Spinner_spinnerMode, MODE_DIALOG);
}
switch (mode) {
case MODE_DIALOG: {// 当SpinnerMode为dialog时
mPopup = new DialogPopup();
mPopup.setPromptText(a.getString(R.styleable.Spinner_prompt));
break;
}
case MODE_DROPDOWN: {// 当SpinnerMode为dropDown时
// 在这里新建了一个DropdownPopup用来展示下拉列表的内容,关于DropdownPopup源码请看下一片段
final DropdownPopup popup = new DropdownPopup(
mPopupContext, attrs, defStyleAttr, defStyleRes);
final TypedArray pa = mPopupContext.obtainStyledAttributes(
attrs, R.styleable.Spinner, defStyleAttr, defStyleRes);
mDropDownWidth = pa.getLayoutDimension(R.styleable.Spinner_dropDownWidth,
ViewGroup.LayoutParams.WRAP_CONTENT);
if (pa.hasValueOrEmpty(R.styleable.Spinner_dropDownSelector)) {
popup.setListSelector(pa.getDrawable(
R.styleable.Spinner_dropDownSelector));
}
popup.setBackgroundDrawable(pa.getDrawable(R.styleable.Spinner_popupBackground));
popup.setPromptText(a.getString(R.styleable.Spinner_prompt));
pa.recycle();
mPopup = popup;
mForwardingListener = new ForwardingListener(this) {
@Override
public ShowableListMenu getPopup() {
return popup;
}
@Override
public boolean onForwardingStarted() {
if (!mPopup.isShowing()) {
mPopup.show(getTextDirection(), getTextAlignment());
}
return true;
}
};
break;
}
}
mGravity = a.getInt(R.styleable.Spinner_gravity, Gravity.CENTER);
mDisableChildrenWhenDisabled = a.getBoolean(
R.styleable.Spinner_disableChildrenWhenDisabled, false);
a.recycle();
// Base constructor can call setAdapter before we initialize mPopup.
// Finish setting things up if this happened.
if (mTempAdapter != null) {
setAdapter(mTempAdapter);
mTempAdapter = null;
}
}