查看AdapterView 中的源码可以发现 根上的原因是:
/** * Called after layout to determine whether the selection position needs to * be updated. Also used to fire any pending selection events. */ void checkSelectionChanged() { if ((mSelectedPosition != mOldSelectedPosition) || (mSelectedRowId != mOldSelectedRowId)) { selectionChanged(); mOldSelectedPosition = mSelectedPosition; mOldSelectedRowId = mSelectedRowId; } // If we have a pending selection notification -- and we won't if we // just fired one in selectionChanged() -- run it now. if (mPendingSelectionNotifier != null) { mPendingSelectionNotifier.run(); } }
所以修改mOldSelectedPosition可以曲线救国。
上代码:
private void setRefresh(Spinner target){ try { Field positionField = AdapterView.class.getDeclaredField("mOldSelectedPosition"); positionField.setAccessible(true); positionField.setInt(target, AdapterView.INVALID_POSITION); //设置mOldSelectedPosition的值 } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }