简体   繁体   English

自定义微调器:setSelection向下滚动

[英]Custom spinner: setSelection scrolling down

I have a custom spinner I have a Hint label that is in the last position of my array(spinner), so to display it I set selection to the last position, like this: 我有一个自定义微调器,我的提示标签位于数组(微调器)的最后一个位置,因此要显示它,请将选择设置为最后一个位置,如下所示:

ArrayAdapter myAdapter = new MySpinnerAdapter(this,R.layout.spinner_item,createMyList());
myAdapter.setDropDownViewResource(spinner_item);
mySpinner.setAdapter(subCategoryAdapter);
 mySpinner.setSelection(myList.size() - 1);

It's working perfectly, but when I touch on Spinner to select a item, the scroll it's "focusing" the bottom of spinner, because of my setSelection. 它工作正常,但是由于我的setSelection,当我触摸微调框以选择一个项目时,滚动将其“聚焦”于微调框的底部。

How can I focus on the first item of the spinner OR "scroll" to the top of it? 如何关注微调器的第一项或“滚动”至其顶部?

Thanks! 谢谢!

You can achieve this by extending Spinner and overriding methods that are responsible for setup and showing the list of values in the drop down: 您可以通过扩展负责设置的Spinner和覆盖方法并在下拉列表中显示值列表来实现此目的:

public class CustomSpinner extends Spinner {

    private boolean mToggleFlag = true;

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

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

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

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

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

    @Override
    public int getSelectedItemPosition() {
        if (!mToggleFlag) {
            return 0; // Gets to the first element
        }
        return super.getSelectedItemPosition();
    }

    @Override
    public boolean performClick() {
        mToggleFlag = false;
        boolean result = super.performClick();
        mToggleFlag = true;
        return result;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM