1.设置第一次不触发点击事件:
mSpinner.setSelection(0, true);
2.响应同一Item事件:
原因在AbsSpinner源码中:
/**
* Makes the item at the supplied position selected.
*
* @param position Position to select
* @param animate Should the transition be animated
*
*/
void setSelectionInt(int position, boolean animate) {
if (position != mOldSelectedPosition) {
mBlockLayoutRequests = true;
int delta = position - mSelectedPosition;
setNextSelectedPositionInt(position);
layout(delta, animate);
mBlockLayoutRequests = false;
}
}
自定义Spinner:
import android.content.Context;
import android.support.v7.widget.AppCompatSpinner;
import android.util.AttributeSet;
/**
* ----------Dragon be here!----------/
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* ━━━━━━神兽出没━━━━━━
*
* @author Reginer in 2017/1/3 9:46.
* QQ:282921012
* Description:点击相同item触发点击事件Spinner
*/
public class ReselectSpinner extends AppCompatSpinner {
public ReselectSpinner(Context context) {
super(context);
}
public ReselectSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ReselectSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setSelection(int position) {
if (getOnItemSelectedListener() != null) {
getOnItemSelectedListener().onItemSelected(this, getSelectedView(),
position, getSelectedItemId());
}
}
}
ps:不是AppcompatActivity继承Spinner。