问题描述
我知道这个问题已经回答了多次,但我没有发现任何人满意,当然不优雅。
I know that this question has been answered multiple times, but I have not found any of them satisfactory and certainly not elegant.
问题是OnItemSelected被炒鱿鱼不仅当用户选择一个项目,还能当选择编程设定。
The issue is that OnItemSelected gets fired not only when the user selects an item, but also when the selection is programmatically set.
一些答案建议设置一个标志时候,程序员设置微调值。但是有时候其他Android code将设置你的code之外的价值。
Some answers propose setting a flag for times when the programmer is setting a value for the spinner. However sometimes other android code will set the value outside of your code.
针对Android设置的值一个共同的地方就是在微调的实例。一些答案解决特定的问题。不过,也有很多地方的Android将打破和再次好手。这是不优雅追查他们。
A common place for android to set the value is upon instantiation of the spinner. Some answers address that particular issue. However, there are numerous places where Android will break down and reinstantiate a spinner. It is not elegant to track down all of them.
所以,问题是:一个人如何重视自己的OnItemSelectedListener code仅通过用户交互的UI做出的选择。
So the question is: how does one attach their OnItemSelectedListener code ONLY to selections made by user interaction with the UI?
- Spinner.setOnItemClickListener似乎将完美地回答这个问题,但谷歌有它禁用
- AdapterView.setOnClickListener也似乎是一个自然的候选,但它也产生一个运行时错误
其次是延长微调,并开始覆盖方法,但是这当然意味着这些API搞乱(我宁愿不做,或者我至少希望有其他用户就可以与我)
Next place is to extend Spinner and start overriding methods but that of course means messing with the APIs (which I'd rather not do, or I'd at least like to have other users working on it with me)
推荐答案
所以我张贴的答案,但任何批评,改进,或其他更优雅的答案是值得欢迎的。
So I am posting an answer, but any criticisms, improvements, or other more elegant answers are welcome.
关键是压倒一切的onClick设置它关系的onItemSelectedListener用户交互,并触发onItemClickedListener的标志。
The key is overriding onClick to set a flag which ties the onItemSelectedListener to user interaction and fires the onItemClickedListener.
如果你不使用API setOnItemClickedListener(或许为未来的兼容性)感觉很舒服,你当然可以代替你自己的方法。我只是觉得应该onItemClickedListener已经实现这种效果的全部时间,所以这是我的微妙的抗议。
If you don't feel comfortable using the API setOnItemClickedListener (for future compatibility perhaps), you can of course substitute your own method. I just felt like onItemClickedListener should have been implemented to this effect the whole time, so that is my subtle protest.
此外,如果任何人都可以想到的一个原因,spinnerTouched标志被短路(停留超过它应该不再如此),请让我们知道,以便它可以得到解决。它似乎工作pretty以及到目前为止虽然。
Also, if anyone can think of a reason that the spinnerTouched flag gets short circuited (stays true for longer than it should), please let us know so that it can be addressed. It seems to work pretty well so far though.
public class OnItemClickSpinner extends Spinner implements AdapterView.OnItemSelectedListener {
boolean spinnerTouched = false;
OnItemClickListener onItemClickListener = null;
OnItemSelectedListener onItemSelectedListener = null;
public OnItemClickSpinner(Context context) {
super(context);
super.setOnItemSelectedListener(this);
}
public OnItemClickSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
super.setOnItemSelectedListener(this);
}
public OnItemClickSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
super.setOnItemSelectedListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
spinnerTouched = true;
return super.onTouchEvent(event);
}
@Override
public void setOnItemClickListener(OnItemClickListener listener) {
onItemClickListener = listener;
}
@Override
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
super.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinnerTouched && this.onItemClickListener!=null) this.onItemClickListener.onItemClick(parent,view,position,id);
if(this.onItemSelectedListener!=null) this.onItemSelectedListener.onItemSelected(parent,view,position,id);
spinnerTouched=false;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
if(this.onItemSelectedListener!=null) this.onItemSelectedListener.onNothingSelected(parent);
spinnerTouched=false;
}
}
这篇关于Android的微调仅OnItemSelected的用户交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!