Android 微调器-聚焦到第一项

Android 微调器-聚焦到第一项,android,android-layout,android-widget,android-spinner,Android,Android Layout,Android Widget,Android Spinner,我使用带有光标适配器的下拉微调器。它包含例如1-100个项目。 我选择例如项目50。项目被选中。下次打开微调器时,第一个可见行是项目50。当我打开微调器时,它将聚焦到第一个项目/第一个可见项目将是项目1,如何实现这一点 我的意思是像列表中的自动滚动一样,下拉列表中的第一个可见项是第一个,而不是选定项。您可以将微调器的选择设置为第一个项,如下所示: yourspinner.setSelection(0); 您可能希望在onStart()方法中执行此操作。这段代码将为您完成这项工作 int

我使用带有光标适配器的下拉微调器。它包含例如1-100个项目。 我选择例如项目50。项目被选中。下次打开微调器时,第一个可见行是项目50。当我打开微调器时,它将聚焦到第一个项目/第一个可见项目将是项目1,如何实现这一点


我的意思是像列表中的自动滚动一样,下拉列表中的第一个可见项是第一个,而不是选定项。

您可以将微调器的选择设置为第一个项,如下所示:

yourspinner.setSelection(0);

您可能希望在onStart()方法中执行此操作。

这段代码将为您完成这项工作

    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);
        }
    });
int-prevSelection=0;
spSunFrom=(微调器)findviewbyd(R.id.spTimeFromSun);
setOnTouchListener(新的OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
//TODO自动生成的方法存根
prevSelection=spSunFrom.getSelectedItemPosition();
spSunFrom.setSelection(0);
返回false;
}
});
spSunFrom.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
如果(arg2==0)
spSunFrom.setSelection(预先选择);
prevSelection=arg2;
}
@凌驾
未选择公共无效(AdapterView arg0){
spSunFrom.setSelection(预先选择);
}
});

您可以通过扩展
微调器并覆盖负责设置/显示值列表的两个方法,使其执行所需操作:

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时,你会在顶部看到第一项是的,但要显示第一项..我找到的唯一方法是:)我不知道我需要做什么,但在android gmail应用程序中它是这样工作的。如果您在导航中有更多在屏幕上可见的项目(例如,在横向模式下),则每次打开微调器时,它都会显示topI上的第一个项目我一直在寻找类似的项目。。。3年后,它仍然是好信息!谢谢你,巴德,太好了!。非常感谢你!