Spinnerpublic class Spinner extends AbsSpinner implements android.content.DialogInterface.OnClickListenerA view that displays one child at a time and lets the user pick among them.
The items in the Spinner come from the {@link Adapter} associated with
this view. |
Fields Summary |
---|
private CharSequence | mPrompt |
Constructors Summary |
---|
public Spinner(android.content.Context context)
this(context, null);
| public Spinner(android.content.Context context, android.util.AttributeSet attrs)
this(context, attrs, com.android.internal.R.attr.spinnerStyle);
| public Spinner(android.content.Context context, android.util.AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.Spinner, defStyle, 0);
mPrompt = a.getString(com.android.internal.R.styleable.Spinner_prompt);
a.recycle();
|
Methods Summary |
---|
public int | getBaseline()
View child = null;
if (getChildCount() > 0) {
child = getChildAt(0);
} else if (mAdapter != null && mAdapter.getCount() > 0) {
child = makeAndAddView(0);
// TODO: We should probably put the child in the recycler
}
if (child != null) {
return child.getTop() + child.getBaseline();
} else {
return -1;
}
| public java.lang.CharSequence | getPrompt()
return mPrompt;
| void | layout(int delta, boolean animate)Creates and positions all views for this Spinner.
int childrenLeft = mSpinnerPadding.left;
int childrenWidth = mRight - mLeft - mSpinnerPadding.left - mSpinnerPadding.right;
if (mDataChanged) {
handleDataChanged();
}
// Handle the empty set by removing all views
if (mItemCount == 0) {
resetList();
return;
}
if (mNextSelectedPosition >= 0) {
setSelectedPositionInt(mNextSelectedPosition);
}
recycleAllViews();
// Clear out old views
removeAllViewsInLayout();
// Make selected view and center it
mFirstPosition = mSelectedPosition;
View sel = makeAndAddView(mSelectedPosition);
int width = sel.getMeasuredWidth();
int selectedOffset = childrenLeft + (childrenWidth / 2) - (width / 2);
sel.offsetLeftAndRight(selectedOffset);
// Flush any cached views that did not get reused above
mRecycler.clear();
invalidate();
checkSelectionChanged();
mDataChanged = false;
mNeedSync = false;
setNextSelectedPositionInt(mSelectedPosition);
| private android.view.View | makeAndAddView(int position)Obtain a view, either by pulling an existing view from the recycler or
by getting a new one from the adapter. If we are animating, make sure
there is enough information in the view's layout parameters to animate
from the old to new positions.
View child;
if (!mDataChanged) {
child = mRecycler.get(position);
if (child != null) {
// Position the view
setUpChild(child);
return child;
}
}
// Nothing found in the recycler -- ask the adapter for a view
child = mAdapter.getView(position, null, this);
// Position the view
setUpChild(child);
return child;
| public void | onClick(android.content.DialogInterface dialog, int which)
setSelection(which);
dialog.dismiss();
| protected void | onLayout(boolean changed, int l, int t, int r, int b)
super.onLayout(changed, l, t, r, b);
mInLayout = true;
layout(0, false);
mInLayout = false;
| public boolean | performClick()
boolean handled = super.performClick();
if (!handled) {
handled = true;
Context context = getContext();
final DropDownAdapter adapter = new DropDownAdapter(getAdapter());
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (mPrompt != null) {
builder.setTitle(mPrompt);
}
builder.setSingleChoiceItems(adapter, getSelectedItemPosition(), this).show();
}
return handled;
| public void | setOnItemClickListener(OnItemClickListener l)A spinner does not support item click events. Calling this method
will raise an exception.
throw new RuntimeException("setOnItemClickListener cannot be used with a spinner.");
| public void | setPrompt(java.lang.CharSequence prompt)Sets the prompt to display when the dialog is shown.
mPrompt = prompt;
| public void | setPromptId(int promptId)Sets the prompt to display when the dialog is shown.
mPrompt = getContext().getText(promptId);
| private void | setUpChild(android.view.View child)Helper for makeAndAddView to set the position of a view
and fill out its layout paramters.
// Respect layout params that are already in the view. Otherwise
// make some up...
ViewGroup.LayoutParams lp = child.getLayoutParams();
if (lp == null) {
lp = generateDefaultLayoutParams();
}
addViewInLayout(child, 0, lp);
child.setSelected(hasFocus());
// Get measure specs
int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
mSpinnerPadding.top + mSpinnerPadding.bottom, lp.height);
int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
mSpinnerPadding.left + mSpinnerPadding.right, lp.width);
// Measure child
child.measure(childWidthSpec, childHeightSpec);
int childLeft;
int childRight;
// Position vertically based on gravity setting
int childTop = mSpinnerPadding.top
+ ((mMeasuredHeight - mSpinnerPadding.bottom -
mSpinnerPadding.top - child.getMeasuredHeight()) / 2);
int childBottom = childTop + child.getMeasuredHeight();
int width = child.getMeasuredWidth();
childLeft = 0;
childRight = childLeft + width;
child.layout(childLeft, childTop, childRight, childBottom);
|
|