选择微调器而不触发侦听器

时间:2014-01-13 15:37:31

标签: android android-spinner

长版本我的屏幕上有两个微调器,当我选择一个时,我会进行某些操作,而另一个微调器应该显示原始选择,因为设计模式。在这种情况下,我让用户按年份和状态选择列表中的元素。您同意我的观点,当用户选择按年份选择时,关于状态的微调器应显示默认选择(显示所有项目的选项)而不是最后选择的选项。

我想从今天早上开始这样做,但是当我找到这个解决方案时,我确实已经完成了。

短版我想在未点击的微调器上设置选择(0),而不触发其侦听器(顺便说一下,如果触发,它将在第一个微调器上设置setSelection(0))

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    // irrelevant code...

    mYearSpinner.setOnItemSelectedListener(onYearSelected);
    mStateSpinner.setOnItemSelectedListener(onStateSelected);

    // irrelevant code...
}

private AdapterView.OnItemSelectedListener onYearSelected = new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mStateSpinner.setOnItemSelectedListener(null);
        mStateSpinner.setSelection(0);
        mStateSpinner.setOnItemSelectedListener(onStateSelected);

        // Do something...
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) { }
};

private AdapterView.OnItemSelectedListener onStateSelected = new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mYearSpinner.setOnItemSelectedListener(null);
        mYearSpinner.setSelection(0);
        mYearSpinner.setOnItemSelectedListener(onYearSelected);

        // Do something...
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) { }
};

但不!听众会不加思索地开火。

有人知道如何处理这个问题吗? 为什么这个解决方案不起作用?


根据要求,以下是我的onCreateView(...)方法的完整代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    Log.d(TAG, "onCreateView");

    View view = inflater.inflate(R.layout.fragment_career, container, false);

    mExamListView = (ListView) view.findViewById(R.id.exam_list_view);
    mYearSpinner = (Spinner) view.findViewById(R.id.select_by_year_spinner);
    mStateSpinner = (Spinner) view.findViewById(R.id.select_by_state_spinner);
    mLoadingView = view.findViewById(R.id.fragment_career_loading_spinner);

    mExamListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Exam exam = (Exam) parent.getItemAtPosition(position);
            if (exam.isBookable()) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                SessionsDialogFragment.newInstance(exam).show(fm, null);
            }
        }
    });

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> yearAdapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.spinner_year, android.R.layout.simple_spinner_item);

    // Specify the layout to use when the list of choices appears
    yearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> stateAdapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.spinner_state, android.R.layout.simple_spinner_item);

    // Specify the layout to use when the list of choices appears
    stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    mYearSpinner.setAdapter(yearAdapter);
    mStateSpinner.setAdapter(stateAdapter);

    mYearSpinner.setOnItemSelectedListener(onYearSelected);
    mStateSpinner.setOnItemSelectedListener(onStateSelected);

    return view;
}

我尝试实施---建议的内容,我想在此报告发生了什么。

当应用程序启动时,所有两个侦听器只执行一次。 然后,如果我在第一个微调器上选择一个项目(让我们说年份微调器),预期的监听器只执行一次(因为它应该是),所以如果我继续在其上选择项目。 最终,如果我在状态微调器上选择和项目,则以这种方式执行监听器

1. onStateListener
2. onYearListener
3. onStateListener

1 个答案:

答案 0 :(得分:1)

从阅读你的评论听起来你想要从你的第一个微调器onItemClick方法中将第二个微调器的选择设置为0而不触发它的监听器。

如果您保留旋转器处于整数变量的旧位置的记录,我们可以检查位置是否已更改,并且仅在运行代码时才运行。

这样,如果我们在将微调器当前位置更改为0之前将旧位置变量设置为0,则会认为其位置没有改变,因此不会在侦听器中运行代码。

private AdapterView.OnItemSelectedListener onYearSelected = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (lastYearPosition != position) {
        //Do you code here
        lastStatePosition = 0;
        mStateSpinner.setSelection(0);
    }
    lastYearPosition = position;
}

@Override
public void onNothingSelected(AdapterView<?> parent) { }
};

private AdapterView.OnItemSelectedListener onStateSelected = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (lastStatePosition != position) {
        //Do your code here
        lastYearPosition = 0;
        mYearSpinner.setSelection(0);
    }
    lastStatePosition = position;
}