문제

I've decided to create my own custom spinner by extending a TextView and composing a ListPopupWindow. I want to emulate the following functionality of the original Spinner: when the spinner is clicked the drop down list is shown, the second time the spinner is clicked the drop down list is dismissed. But I'm having some trouble, the ListPopupWindow.isShowing() seems to always return false (I've debugged it):

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();
                }
            }
        });
    }

So, each time I click on the spinner the drop down list is shown. It is dismissed when I click on one of the items in the list. The problem seems to be that dropDownPopup.isShowing() always returns false.

도움이 되었습니까?

해결책

By setting dropDownPopup.setModal(true), everything works.

다른 팁

By adding dropDownPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);beforedropDownPopup.show();, it works for me.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top