सब से पहले, मुझे पता है कि यह कई बार कहा गया था, लेकिन नए एंड्रॉयड platfom पर ऐसा लगता है जैसे कि सुझाव दिया समाधान काम नहीं करता लग रहा है (अन्य के रूप में ही कहा)। मुझे यह चाहिए कि मेरा स्पिनर अभी भी OnItem को कॉल करता है तब भी जब उपयोगकर्ता दो बार एक ही आइटम का चयन करता है। मैं इस वर्ग है कि चाल करना चाहिए लगता है प्रबंधित किया है:एंड्रॉयड स्पिनर एक ही आइटम के साथ नहीं कहा 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);
}
}
यह वास्तव में काम करता है, लेकिन यह एक बग है: यह दो बार आइटम फोन पहली बार :( किसी को भी पता कर सकते हैं कि कैसे मैं हल कर सकते हैं ?। इस
धन्यवाद साथियों
को कई घंटे अनुसंधान करने के बाद अंत में अपने जवाब मिल गया। आपने अपना समय बहुत बचाया धन्यवाद दोस्त। – Hesam
मैं अपनी गतिविधि में इसका उपयोग कैसे कर सकता हूं? –
@ हम्मानासिर यह सिर्फ एक कस्टम स्पिनर है। क्लासिक स्पिनर के बजाय बस एनडीएसपीनर ऑब्जेक्ट का उपयोग करें। –