Java Android Spinner不适用于使用Android 5.0的三星设备

Java Android Spinner不适用于使用Android 5.0的三星设备,java,android,spinner,android-5.0-lollipop,Java,Android,Spinner,Android 5.0 Lollipop,我正在使用一个自定义微调器小部件,代码如下。 除了三星的安卓5.0设备外,其他设备都可以正常工作。单击微调器时,应显示一个值列表,但这不会发生。 在仿真器和其他品牌的Android 5.0设备上,它运行良好 有没有人遇到过类似的问题,或者对可能发生的事情有什么想法 xml 爪哇 公共类ComboBoxView扩展了LinearLayout{ 私人纺纱机; 私人监听者选择监听者; 公共ComboBoxView(上下文){ 超级(上下文); 初始化(上下文); } 公共ComboxVie

我正在使用一个自定义微调器小部件,代码如下。 除了三星的安卓5.0设备外,其他设备都可以正常工作。单击微调器时,应显示一个值列表,但这不会发生。

在仿真器和其他品牌的Android 5.0设备上,它运行良好

有没有人遇到过类似的问题,或者对可能发生的事情有什么想法

xml



爪哇

公共类ComboBoxView扩展了LinearLayout{
私人纺纱机;
私人监听者选择监听者;
公共ComboBoxView(上下文){
超级(上下文);
初始化(上下文);
}
公共ComboxView(上下文、属性集属性){
超级(上下文,attrs);
初始化(上下文);
}
@TargetApi(构建版本代码蜂窝)
公共ComboBoxView(上下文上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
初始化(上下文);
}
@TargetApi(Build.VERSION\u code.LOLLIPOP)
公共ComboBoxView(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
初始化(上下文);
}
//内部方法:
/**
*初始化布局
*
*@param上下文
*/
私有void initializeLayout(最终上下文){
mListener=null;
//充气并检索视图:
此.setOrientation(LinearLayout.VERTICAL);
LayoutFlater.from(上下文)。充气(R.layout.view\u组合框,此);
mSpinner=(微调器)findViewById(R.id.\u组合\u微调器);
//完成初始化:
final int paddingTop=(int)getResources().getDimension(R.dimen.cell\u text\u section\u text\u padding\u top);
final int paddingBottom=(int)getResources().getDimension(R.dimen.cell\u text\u section\u text\u padding\u bottom);
final int paddingLeft=(int)getResources().getDimension(R.dimen.cell\u text\u section\u text\u padding\u left);
final int paddingRight=(int)getResources().getDimension(R.dimen.cell\u text\u section\u text\u padding\u right);
SetOnClick侦听器(onClick);
设置方向(线性布局。水平);
setBackgroundResource(R.drawable.button_primary);
可点击设置(真);
设置填充(填充左侧、填充顶部、填充右侧、填充底部);
}
@凌驾
公共布尔值onInterceptTouchEvent(MotionEvent ev){
返回true;
}
private final OnClickListener onClick=new OnClickListener(){
@凌驾
公共void onClick(视图v){
mSpinner.performClick();
}
};
@凌驾
public void clearFocus(){
super.clearFocus();
mSpinner.clearFocus();
}
//外部方法:
/**
*调用时要调用的回调的接口定义
*已选择此视图中的一个项目(从{@link AdapterView.OnItemSelectedListener}中提取)。
*/
公共接口OnItemSelectedListener{
/**
*当此视图中的项已被删除时调用的回调方法
*已选择。仅当新选择的
*位置与先前选择的位置不同,或者
*没有选定的项目

*

*如果需要访问,推动器可以调用getItemAtPosition(position) *与所选项目关联的数据。 * *@param parent发生选择的ComboBoxView *@param position视图在适配器中的位置 *@param id所选项目的行id */ 已选择无效项(ComboBoxView父项、整型位置、长id); /** *当选择从此列表中消失时要调用的回调方法 *视图。例如,当触摸被激活时,选择可能会消失 *或者当适配器变空时。 * *@param parent当前不包含选定项的ComboBoxView。 */ 未选择无效(ComboBoxView父项); } public void setvaluesString(最终列表newValues){ setValuesAsString(新值,0); } public void setValuesString(最终列表newValues,int initialValue){ 列表结果=新的ArrayList(newValues.size()); for(字符串值:newValues){ 结果:增加(价值); } 设置值(结果、初始值); } public void setValues(最终列表newValues){ 设置值(newValues,0); } public void setValues(最终列表newValues,int initialValue){ 如果((initialValue>=newValues.size())| |(initialValue<-1)){ IllegalArgumentException ex=新的IllegalArgumentException(“初始值无效”); LOG.error(LOG.SOURCE.UI,“无效”,ex); 掷骰子; } //准备元素列表: //注意:ComboBoxArrayAdapter中的最后一项必须为空。项还应包含 //与“最高”条目相同的行数: 最终列表finalValues=newarraylist(newValues.size()); finalValues.addAll(newValues); int maxLines=1; for(字符序列文本:newValues){ 最终字符串[]行=text.toString().split(“\r\n | \r | \n”); maxLines=Math.max(maxLines,lines.length); } 最终价值。添加(“”); //准备微调器: final ComboBoxArrayAdapter=新ComboxArrayAdapter(this.getContext(),R.layout.view\u combo\u box\u项,最终值); adapter.setDropDownViewResource(R.layout.view\u组合框\u项目\u下拉列表); 适配器.setMaxLines(maxLines); mSpinner.setOnItemSelectedListener(null); mSpinner.setAdapter(适配器); mSpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){ 布尔值firstSelection=true; @凌驾 已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){ if(mListener!=null){ int index=(位置>=(mSpinner.getCount()-1))?-1:位置; mListener.onItemSelected(ComboBoxView.this,索引,id); } } @凌驾 未选择公共无效(AdapterView父级)

<?xml version="1.0" encoding="utf-8"?>
<Spinner
    android:id="@+id/_combo_spinner"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:focusable="false"
    android:background="@null"
    android:clickable="false"
    android:paddingBottom="@dimen/cell_text_section_text_padding_bottom"
    android:paddingLeft="@dimen/cell_text_section_text_padding_left"
    android:paddingRight="@dimen/cell_text_section_text_padding_right"
    android:paddingTop="@dimen/cell_text_section_text_padding_top"
    android:spinnerMode="dropdown" />

<View
    android:layout_width="@dimen/drawable_stroke_width"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="3dp"
    android:background="@color/stroke_dark_grey"
    android:paddingBottom="@dimen/cell_text_section_text_padding_bottom"
    android:paddingTop="@dimen/cell_text_section_text_padding_top" />

<ImageView
    style="@style/image__default"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="center"
    android:layout_marginLeft="@dimen/cell_text_section_text_padding_left"
    android:layout_marginRight="@dimen/cell_text_section_text_padding_right"
    android:src="@drawable/ic_action_expand" />
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 {

    /**
     * <p>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.</p>
     * <p/>
     * 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<String> newValues) {
    setValuesAsString(newValues, 0);
}

public void setValuesAsString(final List<String> newValues, int initialValue) {

    List<CharSequence> result = new ArrayList<CharSequence>(newValues.size());
    for(String value : newValues) {
        result.add(value);
    }
    setValues(result, initialValue);
}

public void setValues(final List<CharSequence> newValues) {
    setValues(newValues, 0);
}

public void setValues(final List<CharSequence> 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<CharSequence> finalValues = new ArrayList<CharSequence>(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;
}
private final OnClickListener onClick = new OnClickListener() {

    @Override
    public void onClick(View v) {
         mSpinner.performClick();
    }
};
android:clickable="true"