Frage

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.

War es hilfreich?

Lösung

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

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top