当进行相同的项目选择时,android spinner fire事件

Con*_*oid 12 android spinner

我想在微调器中选择相同的项目时触发事件.方法

@Override
    public void onItemSelected(AdapterView<?> parent, View arg1, int position,
            long arg3) {
    }
Run Code Online (Sandbox Code Playgroud)

只有在我们做出不同的选择时才会被调用.我的目的是当选择任何项目时重新选择相同的项目或进行不同的选择时显示祝酒词.

@Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
Run Code Online (Sandbox Code Playgroud)

上面的方法并没有解决我的问题.

Sua*_*GLU 18

我发现在旋转器的层次结构中,旧的选择被保存在名为mOldSelectedPosition的变量中.Spinner正在使用此值来检查是否选择了相同的项目,如果它是相同的,则忽略它.如果我们不想忽略这个我做的是一些使用反射的脏代码.

package com.aradiom.amc.nativecomponents;

import java.lang.reflect.Field;

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

public class SpinnerTrigger extends Spinner {

public SpinnerTrigger(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
public void setSelection(int position, boolean animate) {
    ignoreOldSelectionByReflection();
    super.setSelection(position, animate);
}

private void ignoreOldSelectionByReflection() {
    try {
        Class<?> c = this.getClass().getSuperclass().getSuperclass().getSuperclass();
        Field reqField = c.getDeclaredField("mOldSelectedPosition");
        reqField.setAccessible(true);
        reqField.setInt(this, -1);
    } catch (Exception e) {
        Log.d("Exception Private", "ex", e);
        // TODO: handle exception
    }
}

@Override
public void setSelection(int position) {
    ignoreOldSelectionByReflection();
    super.setSelection(position);
}

}
Run Code Online (Sandbox Code Playgroud)

此类将始终使oldselection值无效,以便每次触发click事件.它可能不是完美的解决方案.谨慎使用.:)


Eri*_*ong 8

希望这有帮助.我试过,它的工作原理

/** 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());
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)


小智 1

使用点击监听器来满足您的要求。由于不支持微调器上的直接单击侦听器,因此创建一个类扩展微调器并覆盖单击方法,并在此方法中执行您想要执行的操作。