重复选择微调框当前选择的项目

时间:2019-06-11 05:26:09

标签: android spinner

即使重复选择微调框的当前选择的项目,我也希望发生该事件。例如,如果选择0并再次选择0,我想使事件再次飞行。

public class MySpinner extends Spinner{

OnItemSelectedListener listener;

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

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

if (position == getSelectedItemPosition())
{
listener.onItemSelected(null, null, position, 0);
} 
}

public void setOnItemSelectedListener(OnItemSelectedListener listener)
{
this.listener = listener;
}
}

我使用此代码定义扩展类。您如何将其应用于现有的微调器?

以下是当前来源。

public static TextView mTXT_DRS4DL, mTXT_DRS4D_NXT;
public static Spinner mSPI_DRS4DL, mSPI_DRS4D_NXT;
public static ArrayList<String> mLIST_DRS4DL, mLIST_DRS4D_NXT;
public static ArrayAdapter<String> mADAPTER_DRS4DL, mADAPTER_DRS4D_NXT;
private static boolean isFirstSelected = true;
private static String mCheckString = "";

{

    mSPI_DRS4DL = PlatMainActivity.instance.findViewById(R.id.spi_DRS4DL);
    mLIST_DRS4DL = new ArrayList( Arrays.asList());
    mADAPTER_DRS4DL = new ArrayAdapter<>(PlatMainActivity.instance, R.layout.support_simple_spinner_dropdown_item, mLIST_DRS4DL);
    mSPI_DRS4DL.setAdapter(mADAPTER_DRS4DL);

    mSPI_DRS4D_NXT = PlatMainActivity.instance.findViewById(R.id.spi_DRS4D_NXT);
    mLIST_DRS4D_NXT = new ArrayList( Arrays.asList());
    mADAPTER_DRS4D_NXT = new ArrayAdapter<>(PlatMainActivity.instance, R.layout.support_simple_spinner_dropdown_item, mLIST_DRS4D_NXT);
    mSPI_DRS4D_NXT.setAdapter(mADAPTER_DRS4D_NXT);
}
public static void Search(String mSearch)
{
    String mRemoveName;

    if(mSearch.contains("DRS4DL")) {
        mRemoveName = mSensorSearch.replaceAll(":DRS4DL", "");
        mADAPTER_DRS4DL.add(mRemoveSensorName);
        mADAPTER_DRS4DL.notifyDataSetChanged();
    }
    else if(mSearch.contains("DRS4D-NXT")) {
        mRemoveName = mSensorSearch.replaceAll(":DRS4D-NXT", "");
        mADAPTER_DRS4D_NXT.add(mRemoveSensorName);
        mADAPTER_DRS4D_NXT.notifyDataSetChanged();
    }
}
public static void SensorSpinnerEvent()
{
    mSPI_DRS4DL.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            //AbstractDataInterface.SendCmdMessage(mMessage, AbstractDataInterface.SEND_TO_DATA, PlatCmdMessage.MsgTouch.Req.SelRadar, 0);
        }

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

        }
    });

    mSPI_DRS4D_NXT.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if(isFirstSelected)
            {
                isFirstSelected = false;
            } else {
                switch (position) {
                    case 0:
                        mCheckString = mLIST_DRS4D_NXT.get(0);
                        Log.e("LOG", "---------------------------------------------------->> DRS4D_NXT Check String : " + mCheckString);
                        break;

                    case 1:
                        mCheckString = mLIST_DRS4D_NXT.get(1);
                        Log.e("LOG", "---------------------------------------------------->> DRS4D_NXT Check String : " + mCheckString);
                       break;

                    case 2:
                        mCheckString = mLIST_DRS4D_NXT.get(2);
                        Log.e("LOG", "---------------------------------------------------->> DRS4D_NXT Check String : " + mCheckString);
                        break;
                }
            }
        }

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

        }
    });
}

1 个答案:

答案 0 :(得分:0)

在您的xml中制作widget中的MySpinner。然后替换

public static Spinner mSPI_DRS4DL, mSPI_DRS4D_NXT;

使用

public static MySpinner mSPI_DRS4DL, mSPI_DRS4D_NXT;