2012-09-21 63 views
16

我使用下拉微調與光標適配器。它包含例如1 - 100項。 我選擇了例如項目50.選擇項目。下一次,當我打開微調第一個可見行是項目50.我怎麼能達到這一點,當我打開微調它將集中到第一個項目/第一個可見項目將項目1?微調器 - 重點對第一項

我的意思是像列表中的自動滾動,所以下拉菜單中的第一個可見項目是第一個,沒有選擇一個。

回答

31

可以使Spinner做你想做的通過擴展它和重寫兩種方法,分別負責設置/顯示值列表:

public class CustomSpinnerSelection extends Spinner { 

    private boolean mToggleFlag = true; 

    public CustomSpinnerSelection(Context context, AttributeSet attrs, 
      int defStyle, int mode) { 
     super(context, attrs, defStyle, mode); 
    } 

    public CustomSpinnerSelection(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public CustomSpinnerSelection(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomSpinnerSelection(Context context, int mode) { 
     super(context, mode); 
    } 

    public CustomSpinnerSelection(Context context) { 
     super(context); 
    } 

    @Override 
    public int getSelectedItemPosition() { 
     // this toggle is required because this method will get called in other 
     // places too, the most important being called for the 
     // OnItemSelectedListener 
     if (!mToggleFlag) { 
      return 0; // get us to the first element 
     } 
     return super.getSelectedItemPosition(); 
    } 

    @Override 
    public boolean performClick() { 
     // this method shows the list of elements from which to select one. 
     // we have to make the getSelectedItemPosition to return 0 so you can 
     // fool the Spinner and let it think that the selected item is the first 
     // element 
     mToggleFlag = false; 
     boolean result = super.performClick(); 
     mToggleFlag = true; 
     return result; 
    } 

} 

這應該只是罰款什麼你想要做的。

+2

很好的答案!謝謝! – vandzi

+1

我一直在尋找這樣的... 3年後,它仍然是很好的信息!感謝芽。 – BinaryShrub

+0

太棒了!非常感謝! – sonnv1368

2

可以Spinner的選擇設置這樣的第一個項目:

yourspinner.setSelection(0); 

您可能要做到這一點的在onStart()方法。

+4

此選擇第一項。我不想選擇第一項。只有在微調器中滾動到第一項,才能在下拉列表中看到第一項。 – vandzi

1

這段短小的代碼將爲您完成這項工作。

int prevSelection=0; 
    spSunFrom = (Spinner) findViewById(R.id.spTimeFromSun); 
    spSunFrom.setOnTouchListener(new OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      prevSelection = spSunFrom.getSelectedItemPosition(); 
      spSunFrom.setSelection(0); 
      return false; 
     } 
    }); 
    spSunFrom.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      if(arg2==0) 
       spSunFrom.setSelection(prevSelection); 
      prevSelection = arg2; 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      spSunFrom.setSelection(prevSelection); 
     } 
    }); 
+1

不錯的代碼,但不完全符合我的要求。我不想選擇第一項。我只想滾動到第一項。所以當你打開微調,你會看到第一個項目頂部 – vandzi

+0

是的,但要顯示第一個項目..這是我發現的唯一方法:) – MKJParekh

+0

我不知道什麼是我需要做的魔術,但它的作品在Android的Gmail應用程序的方式。如果您在導航中有更多可在屏幕上顯示的項目(例如,在橫向模式下),則每次打開微調器時,它都會向您顯示頂部的第一項 – vandzi

相關問題