【问题标题】:Spinner - focus to first itemSpinner - 关注第一个项目
【发布时间】:2012-09-13 18:10:09
【问题描述】:

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

我的意思是像在列表中自动向上滚动一样,所以下拉列表中的第一个可见项目是第一个而不是选中的。

【问题讨论】:

    标签: android android-layout android-widget android-spinner


    【解决方案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;
        }
    
    }
    

    它应该适合你想做的事情。

    【讨论】:

    • 我一直在寻找这样的东西...... 3 年后,它仍然是很好的信息!谢谢老兄。
    • 太棒了!非常感谢!
    【解决方案2】:

    您可以像这样将 Spinner 的选择设置为第一项:

    yourspinner.setSelection(0);
    

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

    【讨论】:

    • 这里选择第一项。我不想选择第一项。只在微调器中滚动到第一项,所以我可以在下拉列表中看到第一项。
    【解决方案3】:

    这个简短的代码将为您完成工作。

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

    【讨论】:

    • 不错的代码,但不是我想要的。我不想选择第一项。我只想向上滚动到第一项。因此,当您打开微调器时,您会在顶部看到第一项
    • 是的,但要显示第一项..这是我找到的唯一方法:)
    • 我不知道我需要做什么魔法,但它在 android gmail 应用程序中以这种方式工作。如果您在导航中有更多在屏幕上可见的项目(例如,在横向模式下)每次打开微调器时它会在顶部显示第一个项目
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多