微调器的绑定侦听器以编程方式添加到同一个函数中

时间:2015-06-05 09:57:07

标签: android

我有一个表单,为每个过滤器添加一个新的微调器,每次都使用相同的选项创建。从微调器中选择项目时的功能对于每个微调器都是相同的,所以我在考虑如何将onItemSelected绑定到我创建的每个微调器的相同函数。

我的解决方案是像往常一样设置监听器,并使用重写的onItemSelected的参数调用我的函数。这是正确的,还是有另一种方式?

        // Fix the width of the spinner so it doesn't get resized when making selections.
        // Get the screen width and subtract the width of the button next to the spinner.
        originalAttributeSelector = (Spinner) findViewById(R.id.attributeSelector0);
        originalAttributeSelector.getLayoutParams().width = displayWidth - originalOperatorSelector.getWidth();

        // Create the adapter containing the list of choices for the spinner (as well the style for it)
        // and bind it to the spinner.
        ArrayAdapter<String> attributeSpinnerAdapter =
                new ArrayAdapter<>(this, R.layout.spinner_item_material_ref, filterAttributes);
        attributeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        originalAttributeSelector.setAdapter(attributeSpinnerAdapter);

        originalAttributeSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // This is implemented somewhere else in the class
                myOwnSharedOnItemSelected(parent, view, posiiton, id);
            }

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

            }
        });

0 个答案:

没有答案