我使用下拉微調與光標適配器。它包含例如1 - 100項。 我選擇了例如項目50.選擇項目。下一次,當我打開微調第一個可見行是項目50.我怎麼能達到這一點,當我打開微調它將集中到第一個項目/第一個可見項目將項目1?微調器 - 重點對第一項
我的意思是像列表中的自動滾動,所以下拉菜單中的第一個可見項目是第一個,沒有選擇一個。
我使用下拉微調與光標適配器。它包含例如1 - 100項。 我選擇了例如項目50.選擇項目。下一次,當我打開微調第一個可見行是項目50.我怎麼能達到這一點,當我打開微調它將集中到第一個項目/第一個可見項目將項目1?微調器 - 重點對第一項
我的意思是像列表中的自動滾動,所以下拉菜單中的第一個可見項目是第一個,沒有選擇一個。
可以使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;
}
}
這應該只是罰款什麼你想要做的。
可以Spinner的選擇設置這樣的第一個項目:
yourspinner.setSelection(0);
您可能要做到這一點的在onStart()方法。
此選擇第一項。我不想選擇第一項。只有在微調器中滾動到第一項,才能在下拉列表中看到第一項。 – vandzi
這段短小的代碼將爲您完成這項工作。
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);
}
});
很好的答案!謝謝! – vandzi
我一直在尋找這樣的... 3年後,它仍然是很好的信息!感謝芽。 – BinaryShrub
太棒了!非常感謝! – sonnv1368