android – 如何在相同的微调器值选择上调用操作

前端之家收集整理的这篇文章主要介绍了android – 如何在相同的微调器值选择上调用操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从微调器中选择值,当我再次选择相同的值时,不会对单击执行任何操作.

解决方法

使用这个自定义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,getSelectedItemId());
    }
  }
}
原文链接:https://www.f2er.com/android/308677.html

猜你在找的Android相关文章