我的android应用程序中有一个微调器,用于调用api来更改列表视图中的数据。我已经为此使用了OnItemSelectedListener,但是当我再次单击相同的项目时,没有任何反应,除非我将其更改为其他项目并再次单击我需要的项目。我想使用类似于OnItemClick的东西,因为spinner不支持OnItemClick。请给我一个替代方案。
答案 0 :(得分:0)
从this回答。不幸的是,android Spinner没有提供你想要的功能,但你可以扩展它并手动处理点击:
public class NDSpinner extends Spinner {
public NDSpinner(Context context) {
super(context);
}
public NDSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NDSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void
setSelection(int position, boolean animate) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
@Override
public void setSelection ( int position){
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
}