1)通過延長微調創建一個自定義微調類,
public class CCSpinner extends Spinner {
public CCSpinner(Context context) {
super(context);
}
public CCSpinner(Context context, int mode) {
super(context, mode);
}
public CCSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CCSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CCSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
super(context, attrs, defStyleAttr, mode);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
}
使用上述微調您的XML視圖。
2)定義的接口處理點擊,
public interface ClickListener {
void onItemLongClicked(View view);
void onItemClicked(View view);
}
3)在您的微調適配器類做getView和getDropDownView
這些東西
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//ususal stuffs
convertView.setTag(R.string.click_tag, position);
convertView.setClickable(false);
convertView.setLongClickable(false);
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
//ususal stuffs
convertView.setTag(R.string.click_tag, position);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.onItemClicked(v);
}
}
});
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (clickListener != null) {
clickListener.onItemLongClicked(v);
}
return true;
}
});
return convertView;
}
4)在實例化適配器,通過我們創建的ClcikListener接口。
5),並在實現類做到以下幾點,
@Override
public void onItemLongClicked(final View view) {
mPSpinner.onDetachedFromWindow();
final int pos = (int) view.getTag(R.string.click);
mPSpinner.setSelection(pos);
}
@Override
public void onItemClicked(final View view) {
mPSpinner.onDetachedFromWindow();
int pos = (int) view.getTag(R.string.click);
mPSpinner.setSelection(pos);
}
嘗試交叉檢查你的「微調ID」 /如果你有嵌套視圖,則對佈局禁用重點對微調 – 2014-10-26 21:39:53
得到事件我有一個顯示字符串數組的Spinner(沒有自定義的Spinner實現),我不明白你的意思。你能給我更多的細節嗎? – AndreaF 2014-10-26 21:42:59
你可以請你添加你的代碼調試..我們不能幫你,直到我們可以看到你的執行... – 2014-10-26 21:52:49