为什么我的自定义微调器被禁用/无响应?

时间:2017-07-30 04:39:27

标签: java android android-spinner android-custom-view

我已经创建了一个自定义微调器,因为我一次又一次地发现我想确保在设置Spinner的初始选择或设置新的自定义适配器时未触发onItemSelectedListener。我只想在用户实际选择项目时触发它。

但由于某种原因(我完全失去了原因),我的自定义微调器不响应触摸事件。这就好像它被禁用了,即使我已经调试并看到它完全启用了。但出于某种原因,我的小旋转器不会打开。谁能帮我理解为什么?

这是xml:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="@dimen/default_margin"
        android:orientation="horizontal">
    <my.app.custom.view.MySpinner
        android:id="@+id/dog_or_cat_toggle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:layout_margin="0dp"
        android:textAlignment="center"
        android:gravity="center_vertical|center"
        android:padding="0dp"
        android:entries="@array/dog_or_cat"
        android:spinnerMode="dropdown"
        android:background="@drawable/top_to_bottom_gray_gradient"/>
    ...
</LinearLayout>

我的自定义微调器:

/* A Spinner dispatches an onItemSelected event when the View is initialized, before the user ever makes a selection.
 * This class allows listeners for just the initial selection, just user selections, or both. */
public class MySpinner extends Spinner {
    private boolean initialized = false;
    private OnItemSelectedListener onItemSelectionInitializedListener;
    private OnItemSelectedListener onItemSelectedByUserListener;
    private OnItemSelectedListener onItemSelectedListener;

    public MySpinner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySpinner(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public MySpinner(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.initializeMySpinner();
    }

    public void setOnItemSelectionInitializedListener(OnItemSelectedListener onItemSelectionInitializedListener) {
        this.onItemSelectionInitializedListener = onItemSelectionInitializedListener;
    }

    public void setOnItemSelectedByUserListener(OnItemSelectedListener onItemSelectedByUserListener) {
        this.onItemSelectedByUserListener = onItemSelectedByUserListener;
    }

    @Override
    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        this.onItemSelectedListener = onItemSelectedListener;
    }

    @Override
    public void setAdapter(SpinnerAdapter adapter) {
        this.initialized = false;
        super.setAdapter(adapter);
    }

    private void initializeMySpinner() {
        super.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(!initialized) {
                    if(onItemSelectionInitializedListener != null) onItemSelectionInitializedListener.onItemSelected(parent, view, position, id);
                    if(onItemSelectedListener != null) onItemSelectedListener.onItemSelected(parent, view, position, id);
                    initialized = true;
                } else {
                    if(onItemSelectedListener != null) onItemSelectedListener.onItemSelected(parent, view, position, id);
                    if(onItemSelectedByUserListener != null) onItemSelectedByUserListener.onItemSelected(parent, view, position, id);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                if(!initialized) {
                    if(onItemSelectionInitializedListener != null) onItemSelectionInitializedListener.onNothingSelected(parent);
                    if(onItemSelectedListener != null) onItemSelectedListener.onNothingSelected(parent);
                    initialized = true;
                } else {
                    if(onItemSelectedListener != null) onItemSelectedListener.onNothingSelected(parent);
                    if(onItemSelectedByUserListener != null) onItemSelectedByUserListener.onNothingSelected(parent);
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

不要从另一个构建者调用一个构造函数。相反,从每个调用super()构造函数。

我在一段时间后面临同样的问题,这个伎俩有效,但我不确定原因。