@Override public DualControlLayout getView(int position, View convertView, ViewGroup parent) { DualControlLayout view; if (convertView instanceof DualControlLayout) { view = (DualControlLayout) convertView; } else { view = (DualControlLayout) LayoutInflater.from(getContext()) .inflate(R.layout.infobar_control_spinner_view, parent, false); } // Set up the spinner label. The text it displays won't change. TextView labelView = (TextView) view.getChildAt(0); labelView.setText(mLabel); // Because the values can be of different widths, the TextView may expand or shrink. // Enforcing a minimum width prevents the layout from doing so as the user swaps values, // preventing unwanted layout passes. TextView valueView = (TextView) view.getChildAt(1); valueView.setText(getItem(position).toString()); valueView.setMinimumWidth(mMinWidthRequiredForValues); return view; }
/** * Computes and records the minimum width required to display any of the values without causing * another layout pass when switching values. */ int computeMinWidthRequiredForValues() { DualControlLayout layout = getView(0, null, null); TextView container = (TextView) layout.getChildAt(1); Paint textPaint = container.getPaint(); float longestLanguageWidth = 0; for (int i = 0; i < getCount(); i++) { float width = textPaint.measureText(getItem(i).toString()); longestLanguageWidth = Math.max(longestLanguageWidth, width); } mMinWidthRequiredForValues = (int) Math.ceil(longestLanguageWidth); return mMinWidthRequiredForValues; }