首先,我知道這是問了好幾次,但在較新的android platfom它看起來像建議的解決方案不起作用(如其他人說的一樣)。 我需要我的微調仍然調用OnItemSelected,即使用戶選擇同一個項目兩次。 我設法找到這一類,它應該做的伎倆:Android微調OnItemSelected沒有調用相同的項目
public class NDSpinner extends Spinner {
private int lastSelected = 0;
private static Method s_pSelectionChangedMethod = null;
static {
try {
Class noparams[] = {};
Class targetClass = AdapterView.class;
s_pSelectionChangedMethod = targetClass.getDeclaredMethod("selectionChanged", noparams);
if (s_pSelectionChangedMethod != null) {
s_pSelectionChangedMethod.setAccessible(true);
}
} catch(Exception e) {
Log.e("Custom spinner, reflection bug:", e.getMessage());
throw new RuntimeException(e);
}
}
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
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(this.lastSelected == this.getSelectedItemPosition())
testReflectionForSelectionChanged();
if(!changed)
lastSelected = this.getSelectedItemPosition();
super.onLayout(changed, l, t, r, b);
}
public void testReflectionForSelectionChanged() {
try {
Class noparams[] = {};
s_pSelectionChangedMethod.invoke(this, noparams);
} catch (Exception e) {
Log.e("Custom spinner, reflection bug: ", e.getMessage());
e.printStackTrace();
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
super.onClick(dialog, which);
}
}
這INFACT的作品,但它有一個缺陷:它調用兩次該項目在第一時間:( 能否有人知道如何解決?這
感謝隊友
幾個小時做研究後,終於找到了答案。你節省了很多時間。謝啦。 – Hesam
我如何在我的活動中使用它? –
@HammadNasir它只是一個自定義的微調。只需使用NDSpinner對象而不是經典的Spinner。 –