2013-11-01 10 views
7

prima di tutto, so che questo è stato chiesto più volte, ma il più recente platfom Android sembra che che le soluzioni proposte non funziona (come altro ha detto lo stesso). Ho bisogno che lo spinner continui a chiamare OnItemSelected anche quando l'utente seleziona due volte lo stesso oggetto. sono riuscito a trovare questa classe che dovrebbe fare il trucco:Android Spinner non OnItemSelected chiamato con lo stesso articolo

public class NDSpinner extends Spinner { 

    private int lastSelected = 0; 
    private static Method s_pSelectionChangedMethod = null; 


    static {   
     try { 
      Class noparams[] = {}; 
      Class targetClass = AdapterView.class; 

      s_pSelectionChangedMethod = targetClass.getDeclaredMethod("selectionChanged", noparams);    
      if (s_pSelectionChangedMethod != null) { 
       s_pSelectionChangedMethod.setAccessible(true);    
      } 

     } catch(Exception e) { 
      Log.e("Custom spinner, reflection bug:", e.getMessage()); 
      throw new RuntimeException(e); 
     } 
    } 

    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 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    if(this.lastSelected == this.getSelectedItemPosition()) 
     testReflectionForSelectionChanged(); 
    if(!changed) 
     lastSelected = this.getSelectedItemPosition(); 

    super.onLayout(changed, l, t, r, b); 
} 



    public void testReflectionForSelectionChanged() { 
     try { 
      Class noparams[] = {};   
      s_pSelectionChangedMethod.invoke(this, noparams); 
     } catch (Exception e) { 
      Log.e("Custom spinner, reflection bug: ", e.getMessage()); 
      e.printStackTrace();     
     } 
    } 




    @Override 
    public void onClick(DialogInterface dialog, int which) {  
     super.onClick(dialog, which); 
    } 
} 

Questo infatti funziona, ma ha un bug: si chiama due volte la voce per la prima volta :( Nessuno può sapere come posso risolvere ?. questo

Grazie compagni

risposta

14

ho risolto utilizzando questa classe:

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()); 
     } 
     } 
    } 

Grazie comunque :)

+0

Dopo diverse ore facendo ricerche finalmente trovato la risposta. Mi hai salvato molto tempo. Grazie uomo. – Hesam

+0

come posso usarlo nella mia attività? –

+0

@HammadNasir è solo uno Spinner personalizzato. Basta usare l'oggetto NDSpinner al posto del classico Spinner. –

0

Per me, ho esteso AppCompatSpinner.

Anche se il vostro Spinneris in XML per il layout, di cambiare la propria

<Spinner... 

a

<com.example.util.NDSpinner... 
Problemi correlati