Jeśli chcesz „onItemSelected” z Spinner być zwolniony, nawet jeśli pozycja w turbinki jest wybrana/wybrany element i jeśli jest ona ponownie kliknięciu. Następnie używać tego niestandardowe klasy, która rozciąga się tarczy, to pracował dla mnie
Następnie edytować swoją aktywność z błystki jak ten, zmieniłem
static Spinner spinner1;
do
static NDSpinner spinner1;
i
variables.spinner1 = (Spinner) findViewById(R.id.spinner1);
do
variables.spinner1 = (NDSpinner) findViewById(R.id.spinner1);
Także zmieniłem układ xml gdzie tarczy znajduje
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/place" />
do
<com.yourpackagename.NDSpinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/place" />
klasa
Spinner rozszerzenie:
package com.yourpackagename;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Spinner;
import android.widget.Toast;
import java.lang.reflect.Field;
/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value.
* ie This is extended "Customized class of Spinner" to get the "onItemSelected" event even if the item in the
* Spinner is already selected by the user*/
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); }
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, boolean animate)
{
boolean sameSelected = position == getSelectedItemPosition();
ignoreOldSelectionByReflection();
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) {
ignoreOldSelectionByReflection();
super.setSelection(position);
}
}
Czekaj, opisał _opposite_ jak działa Spinner. Spinners rzucają wyjątek RuntimeException, gdy używa się OnItemClickListener, a OnItemSelectedListener nazywa się _regardless_, niezależnie od tego, czy użytkownik ponownie wybrał poprzedni wybór, czy też dokonał nowego wyboru ... – Sam
Dzięki za opinię, pomyliłem się. Używam onItemSelectedListemer(). Zmieniłem teraz moją wiadomość. – user1400716