[英]Android Spinner On Re-Select Item
我的布局中有一個 微調器。 目前,一切都運行良好,但我需要在重新選擇項目時做一些事情。
有沒有辦法知道微調器項目被重新選擇?
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
中實現了重新選擇回調。 參考: https://stackoverflow.com/a/11323043/9909365
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());
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.