我已经实现了一个扩展BaseAdapter
的自定义微调器,并且我已经覆盖了getView()
和getDropDownView()
,以便为微调器和下拉项提供不同的视图。
如何在打开或关闭微调器的下拉视图时收到通知?
我的自定义微调器类是这样的:
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
}
答案 0 :(得分:0)
在自定义微调器上添加打开和关闭侦听器
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;
public class CustomSpinner extends Spinner {
// private static final String TAG = "CustomSpinner";
private OnSpinnerEventsListener mListener;
private boolean mOpenInitiated = false;
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
super(context, attrs, defStyleAttr, mode);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSpinner(Context context, int mode) {
super(context, mode);
}
public CustomSpinner(Context context) {
super(context);
}
public interface OnSpinnerEventsListener {
void onSpinnerOpened(Spinner spin);
void onSpinnerClosed(Spinner spin);
}
@Override
public boolean performClick() {
// register that the Spinner was opened so we have a status
// indicator for the activity(which may lose focus for some other
// reasons)
mOpenInitiated = true;
if (mListener != null) {
mListener.onSpinnerOpened(this);
}
return super.performClick();
}
public void setSpinnerEventsListener(OnSpinnerEventsListener onSpinnerEventsListener) {
mListener = onSpinnerEventsListener;
}
/**
* Propagate the closed Spinner event to the listener from outside.
*/
public void performClosedEvent() {
mOpenInitiated = false;
if (mListener != null) {
mListener.onSpinnerClosed(this);
}
}
/**
* A boolean flag indicating that the Spinner triggered an open event.
*
* @return true for opened Spinner
*/
public boolean hasBeenOpened() {
return mOpenInitiated;
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if (hasBeenOpened() && hasWindowFocus) {
performClosedEvent();
}
}
}
答案 1 :(得分:0)
因此,您可以在ItemSelectedListener
方法中实现用于打开/关闭微调器的代码。
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
...
}
@Override
public void onNothingSelected(AdapterView<?> parent) { }
});