2013-11-01 8 views
7

सब से पहले, मुझे पता है कि यह कई बार कहा गया था, लेकिन नए एंड्रॉयड platfom पर ऐसा लगता है जैसे कि सुझाव दिया समाधान काम नहीं करता लग रहा है (अन्य के रूप में ही कहा)। मुझे यह चाहिए कि मेरा स्पिनर अभी भी OnItem को कॉल करता है तब भी जब उपयोगकर्ता दो बार एक ही आइटम का चयन करता है। मैं इस वर्ग है कि चाल करना चाहिए लगता है प्रबंधित किया है:एंड्रॉयड स्पिनर एक ही आइटम के साथ नहीं कहा 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); 
    } 
} 

यह वास्तव में काम करता है, लेकिन यह एक बग है: यह दो बार आइटम फोन पहली बार :( किसी को भी पता कर सकते हैं कि कैसे मैं हल कर सकते हैं ?। इस

धन्यवाद साथियों

उत्तर

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

@ हम्मानासिर यह सिर्फ एक कस्टम स्पिनर है। क्लासिक स्पिनर के बजाय बस एनडीएसपीनर ऑब्जेक्ट का उपयोग करें। –

0

मेरे लिए, मैं AppCompatSpinner बढ़ाया।

इसके अलावा, अगर लेआउट के लिए XML में अपनी Spinneris, बदलने के लिए याद अपने

<Spinner... 

<com.example.util.NDSpinner... 
संबंधित मुद्दे