Android Spinner没有使用Android 5.0的三星设备

我正在使用自定义Spinner小部件,其代码如下。 除了三星的Android 5.0设备外,大多数设备都能正常运行。 单击时,微调器应显示值列表但不会发生。

在使用Android 5.0的模拟器和其他品牌设备上,它可以正常工作。

有没有人遇到类似的问题或者对可能发生的事情有任何想法?

XML

 

    

Java的

 public class ComboBoxView extends LinearLayout { private Spinner mSpinner; private OnItemSelectedListener mListener; public ComboBoxView(Context context) { super(context); initializeLayout(context); } public ComboBoxView(Context context, AttributeSet attrs) { super(context, attrs); initializeLayout(context); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public ComboBoxView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initializeLayout(context); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public ComboBoxView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initializeLayout(context); } // Internal methods: /** * Initializes the layout * * @param context */ private void initializeLayout(final Context context) { mListener = null; // Inflate and retrieve the views: this.setOrientation(LinearLayout.VERTICAL); LayoutInflater.from(context).inflate(R.layout.view_combo_box, this); mSpinner = (Spinner) findViewById(R.id._combo_spinner); // Finish initialization: final int paddingTop = (int) getResources().getDimension(R.dimen.cell_text_section_text_padding_top); final int paddingBottom = (int) getResources().getDimension(R.dimen.cell_text_section_text_padding_bottom); final int paddingLeft = (int) getResources().getDimension(R.dimen.cell_text_section_text_padding_left); final int paddingRight = (int) getResources().getDimension(R.dimen.cell_text_section_text_padding_right); setOnClickListener(onClick); setOrientation(LinearLayout.HORIZONTAL); setBackgroundResource(R.drawable.button_primary); setClickable(true); setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return true; } private final OnClickListener onClick = new OnClickListener() { @Override public void onClick(View v) { mSpinner.performClick(); } }; @Override public void clearFocus() { super.clearFocus(); mSpinner.clearFocus(); } // External methods: /** * Interface definition for a callback to be invoked when * an item in this view has been selected (extracted from {@link AdapterView.OnItemSelectedListener}). */ public interface OnItemSelectedListener { /** * 

Callback method to be invoked when an item in this view has been * selected. This callback is invoked only when the newly selected * position is different from the previously selected position or if * there was no selected item.

*

* Impelmenters can call getItemAtPosition(position) if they need to access the * data associated with the selected item. * * @param parent The ComboBoxView where the selection happened * @param position The position of the view in the adapter * @param id The row id of the item that is selected */ void onItemSelected(ComboBoxView parent, int position, long id); /** * Callback method to be invoked when the selection disappears from this * view. The selection can disappear for instance when touch is activated * or when the adapter becomes empty. * * @param parent The ComboBoxView that now contains no selected item. */ void onNothingSelected(ComboBoxView parent); } public void setValuesAsString(final List newValues) { setValuesAsString(newValues, 0); } public void setValuesAsString(final List newValues, int initialValue) { List result = new ArrayList(newValues.size()); for(String value : newValues) { result.add(value); } setValues(result, initialValue); } public void setValues(final List newValues) { setValues(newValues, 0); } public void setValues(final List newValues, int initialValue) { if((initialValue >= newValues.size()) || (initialValue < -1)) { IllegalArgumentException ex = new IllegalArgumentException("Invalid value for initialValue"); LOG.error(LOG.SOURCE.UI, "Invalid",ex); throw ex; } // Prepare the list of elements: // NOTE: The last item in ComboBoxArrayAdapter must be empty. Items should also contain the // same number of lines as the "tallest" entry: final List finalValues = new ArrayList(newValues.size()); finalValues.addAll(newValues); int maxLines = 1; for(CharSequence text : newValues) { final String[] lines = text.toString().split("\r\n|\r|\n"); maxLines = Math.max(maxLines, lines.length); } finalValues.add(""); // Prepare spinner: final ComboBoxArrayAdapter adapter = new ComboBoxArrayAdapter(this.getContext(), R.layout.view_combo_box_item, finalValues); adapter.setDropDownViewResource(R.layout.view_combo_box_item_dropdown); adapter.setMaxLines(maxLines); mSpinner.setOnItemSelectedListener(null); mSpinner.setAdapter(adapter); mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { boolean firstSelection = true; @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { if (mListener != null) { int index = (position >= (mSpinner.getCount() - 1)) ? -1 : position; mListener.onItemSelected(ComboBoxView.this, index, id); } } @Override public void onNothingSelected(AdapterView parent) { if (mListener != null) { mListener.onNothingSelected(ComboBoxView.this); } } }); if (mListener != null) { mListener.onNothingSelected(this); } // Set initial selection: if(initialValue != -1) { mSpinner.setSelection(initialValue); } else { mSpinner.setSelection(newValues.size()); } } public void setOnItemSelectedListener(final OnItemSelectedListener listener) { mListener = listener; } public int getSelectedItem() { int result = mSpinner.getSelectedItemPosition(); if(result >= mSpinner.getCount()) { result = -1; } return result; }

微调
在此处输入图像描述

示例结果
在此处输入图像描述

提前致谢。

我终于修好了!

android属性clickable设置为false ,但是在以下代码的ComboBoxView.java文件中执行了单击行为:

 private final OnClickListener onClick = new OnClickListener() { @Override public void onClick(View v) { mSpinner.performClick(); } }; 

除了在安装了Android 5.0的三星设备上之外,它在各处工作(设备和模拟器)。 我无法弄清楚为什么。

在我将cliclabke属性更改为true后,它开始工作。

 android:clickable="true" 

谢谢。