android - 我怎样才能对相同的微调器值选择调用一个 Action

标签 android android-spinner

我从微调器中选择值,当我再次选择相同的值时,不会对点击执行任何操作。

最佳答案

使用这个自定义的 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());
    }
  }
}

关于android - 我怎样才能对相同的微调器值选择调用一个 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16012470/

相关文章:

java - 检查通用函数android java中的对象类型

iphone - 无法在 Android 或 iPhone 上手动滚动 div

android - Android 微调器内的进度条

android - 如何更改微调器复选框的大小或颜色?

android - 多个微调器上的 OnItemSelectedListener 不工作

java - 微调器禁用滑动打开

安卓layout_gravity

android - 为什么 fresco 在 android 5.0 或更高版本上选择不将位图放入 ashmem

java - Neokree 抽屉导航 : Gradle DSL method not found: 'compile()'

android - 如何将微调器添加到 ActionBar?