2013-11-01 31 views
7

首先,我知道這是問了好幾次,但在較新的android platfom它看起來像建議的解決方案不起作用(如其他人說的一樣)。 我需要我的微調仍然調用OnItemSelected,即使用戶選擇同一個項目兩次。 我設法找到這一類,它應該做的伎倆:Android微調OnItemSelected沒有調用相同的項目

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

這INFACT的作品,但它有一個缺陷:它調用兩次該項目在第一時間:( 能否有人知道如何解決?這

感謝隊友

回答

14

我已經使用這個類來解決:

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

不管怎樣,謝謝:)

+0

幾個小時做研究後,終於找到了答案。你節省了很多時間。謝啦。 – Hesam

+0

我如何在我的活動中使用它? –

+0

@HammadNasir它只是一個自定義的微調。只需使用NDSpinner對象而不是經典的Spinner。 –

0

對我來說,我擴展了AppCompatSpinner。

此外,如果您在XML Spinneris佈局,記得要改變你的

<Spinner... 

<com.example.util.NDSpinner...