2016-07-22 70 views
0

我在我的android應用程序中有一個微調器,用來調用api來更改列表視圖中的數據。我已經使用了OnItemSelectedListener,但是當我再次單擊相同的項目時,沒有任何反應,除非將其更改爲某個其他項目,然後再次單擊我需要的項目。我想使用類似於OnItemClick的東西,因爲Spinner不支持OnItemClick。請給我一個替代方案。微調項目點擊功能被添加

回答

0

this回答。不幸的是,android微調不提供您想要的功能,但您可以擴展它並手動處理點擊:

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

    } 
}