我决定通过扩展TextView并编写ListPopupWindow来创建自己的自定义微调器.我想模仿原始Spinner的以下功能:当单击微调器时,会显示下拉列表,第二次单击微调器时,下拉列表将被取消.但是我遇到了一些麻烦,ListPopupWindow.isShowing()似乎总是返回false(我调试了它):
public class CustomSpinner extends TextView {
...
private ListPopupWindow dropDownPopup;
...
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
...
dropDownPopup = new ListPopupWindow(context, attrs);
dropDownPopup.setAnchorView(this);
dropDownPopup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
dropDownPopup.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
dropDownPopup.dismiss();
...
}
});
this.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dropDownPopup.isShowing()) {
dropDownPopup.dismiss();
} else {
dropDownPopup.show();
}
}
});
}
因此,每次单击微调器时,都会显示下拉列表.单击列表中的某个项目时,它会被取消.问题似乎是dropDownPopup.isShowing()总是返回false.