2013-11-01 23 views
7

trước hết, tôi biết rằng điều này đã được hỏi một thời gian, nhưng trên platfom android mới hơn có vẻ như các giải pháp được đề xuất không hoạt động (như khác nói như nhau). Tôi cần rằng trình quay số của tôi vẫn gọi OnItemSelected ngay cả khi người dùng chọn cùng một mục hai lần. tôi đã quản lý để tìm lớp này mà nên làm các trick:Android Spinner OnItemSelected không được gọi với cùng một mục

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 này hoạt động, nhưng nó có một lỗi: nó gọi gấp đôi mục lần đầu tiên :( Ai có thể biết làm thế nào tôi có thể giải quyết ?. này

Cảm ơn bạn tình

Trả lời

14

tôi đã giải quyết sử dụng lớp này:

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

Thanks anyway :)

+0

Sau vài giờ làm nghiên cứu cuối cùng đã tìm thấy câu trả lời của bạn. Bạn đã tiết kiệm thời gian của tôi rất nhiều. Cảm ơn người đàn ông. – Hesam

+0

Tôi có thể sử dụng điều này như thế nào trong hoạt động của mình? –

+0

@HammadNasir nó chỉ là một Spinner tùy chỉnh. Chỉ cần sử dụng đối tượng NDSpinner thay vì Spinner cổ điển. –

0

Đối với tôi, tôi đã mở rộng AppCompatSpinner.

Ngoài ra nếu Spinneris của bạn trong XML cho bố trí, hãy nhớ thay đổi của bạn

<Spinner... 

để

<com.example.util.NDSpinner... 
Các vấn đề liên quan