简体   繁体   English

Android 微调器重新选择项目

[英]Android Spinner On Re-Select Item

I have a spinner in my layout.我的布局中有一个 微调器 currently, everything is working well but I need to do something when item reselected.目前,一切都运行良好,但我需要在重新选择项目时做一些事情。

Is there any way to know the spinner item is reselected?.有没有办法知道微调器项目被重新选择?

public void onItemSelected(AdapterView<?> parent, View view,
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

I dont know if this idea can help you but you can try to create an ArrayList whit the pos or id values that you get in "onItemSelected", and inside of the method you read the ArrayList searching for it... something like this:我不知道这个想法是否可以帮助您,但您可以尝试创建一个 ArrayList,其中包含您在“onItemSelected”中获得的 pos 或 id 值,并在您阅读 ArrayList 搜索它的方法内部......像这样:

ArrayList<Integer> positions = new ArrayList();
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
   for(int posit : positions){
      if(posit==pos){
         //item reselected
      }
   }
   positions.add(pos);
}

I achieve reselection callback in onItemSelected by this custom spinner code.我通过这个自定义微调器代码在onItemSelected中实现了重新选择回调。 Ref: https://stackoverflow.com/a/11323043/9909365参考: https://stackoverflow.com/a/11323043/9909365

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;


/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */
public class NDSpinner extends Spinner {

    public NDSpinner(Context context)
    { super(context); }

    public NDSpinner(Context context, AttributeSet attrs)
    { super(context, attrs); }

    public NDSpinner(Context context, AttributeSet attrs, int defStyle)
    { super(context, attrs, defStyle); }

    @Override 
    public void setSelection(int position, boolean animate) {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position, animate);
        if (sameSelected) {
            // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
            getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
        }
    } 

    @Override
    public void setSelection(int position) {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position);
        if (sameSelected) {
            // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
            getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM