问题很奇怪,此外还发现适配完数据后会默认选中第一个,并且这个默认选中第一个的操作并不是马上执行的,而是一段时候后再执行,并触发OnItemSelectedListener事件。

 旧代码:

spinner.setAdapter(adapter);
spinner.setSelection(2); 

新代码:

spinner.setAdapter(adapter);
spinner.setSelection(2,true);

  以下是两个函数的源代码:

  /**
     * Jump directly to a specific item in the adapter data.
     */
    public void setSelection(int position, boolean animate) {
        // Animate only if requested position is already on screen somewhere
        boolean shouldAnimate = animate && mFirstPosition <= position &&
                position <= mFirstPosition + getChildCount() - 1;
        setSelectionInt(position, shouldAnimate);
    }
    

    @Override
    public void setSelection(int position) {
        setNextSelectedPositionInt(position);
        requestLayout();
        invalidate();
    }

  看起来像是专门准备了一个函数在数据适配(填充)完后设置默认值的

相关文章:

  • 2022-03-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2022-02-17
  • 2021-12-04
猜你喜欢
  • 2021-09-19
  • 2022-12-23
  • 2021-05-29
  • 2021-05-02
  • 2021-08-10
  • 2022-12-23
  • 2022-01-20
相关资源
相似解决方案