Android 重新 Select 项目上的微调器
Android Spinner On Re-Select Item
我的布局中有一个 spinner。目前,一切正常,但我需要在重新选择项目时做一些事情。
有什么方法可以知道微调项已被重新选择?
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
我不知道这个想法是否对您有帮助,但您可以尝试创建一个 ArrayList,其中包含您在“onItemSelected”中获得的 pos 或 id 值,并在您阅读 ArrayList 搜索它的方法内部。 . 像这样:
ArrayList<Integer> positions = new ArrayList();
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
for(int posit : positions){
if(posit==pos){
//item reselected
}
}
positions.add(pos);
}
我通过这个自定义微调器代码在 onItemSelected
中实现了重选回调。
参考:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;
/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */
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());
}
}
}
我的布局中有一个 spinner。目前,一切正常,但我需要在重新选择项目时做一些事情。
有什么方法可以知道微调项已被重新选择?
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
我不知道这个想法是否对您有帮助,但您可以尝试创建一个 ArrayList,其中包含您在“onItemSelected”中获得的 pos 或 id 值,并在您阅读 ArrayList 搜索它的方法内部。 . 像这样:
ArrayList<Integer> positions = new ArrayList();
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
for(int posit : positions){
if(posit==pos){
//item reselected
}
}
positions.add(pos);
}
我通过这个自定义微调器代码在 onItemSelected
中实现了重选回调。
参考:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;
/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */
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());
}
}
}