Android Spinner没有对任何点击事件做出反应

时间:2015-12-26 19:43:46

标签: android android-layout android-spinner

我构建了一个自定义Spinner,它有一个加载某些数据的方法,一旦加载了这些数据(在ArrayList个事件中),它们就会重新显示。

除了一些烦人的东西外,一切正常:我在这个微调器上检测到任何点击事件(更改所选项目),我的OnItemSelectedListener仅在开始时触发一次......

但是比blabla更好,这是代码:

public class ActionChooser extends Spinner {
    private Context c;
    ArrayList<Event> events;
    OnItemSelectedListener listener;

    public ActionChooser(Context context) {
        super(context);
        this.c = context;
        init();
    }

    public ActionChooser(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.c = context;
        init();
    }


    private void init() {
        // events arrayList is filled by a XML parser there, too long and uninteresting for SO
        setAdapter(new CustomAdapter());
        OnItemSelectedListener l = new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d("ActionChooser", "i:" + i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                Log.d("ActionChooser", "nothing");
            }
        };
        setOnItemSelectedListener(l);
    }


    private class Event {

        String methodSignature;
        String name;
        boolean isBefore;
        ArrayList<Param> data;
    }

    private class Param {
        int pos;
        String name;
    }

    private class CustomAdapter extends BaseAdapter implements SpinnerAdapter {


        @Override
        public int getCount() {
            return events.size();
        }

        @Override
        public Object getItem(int i) {
            return events.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View row = inflate(c, R.layout.row_action_chooser, null);
            TextView tv = (TextView) row.findViewById(R.id.title);
            TextView methodName = (TextView) row.findViewById(R.id.javaM);
            CheckBox cb = (CheckBox) row.findViewById(R.id.checkbox);
            LinearLayout ll = (LinearLayout) row.findViewById(R.id.dataContainer);
            tv.setText(WordUtils.capitalize(events.get(i).name));
            methodName.setText(events.get(i).methodSignature);
            cb.setChecked(events.get(i).isBefore);
            for (Param p : events.get(i).data) {
                TextView pt = (TextView) row.findViewById(R.id.parameterTitle);
                pt.setVisibility(VISIBLE);
                TextView ptv = new TextView(c);
                ptv.setText(p.name + " (pos: " + p.pos + ")");
                ptv.setTextSize(10);
                ll.addView(ptv);
            }
            return row;
        }
    }
}

微调器看起来或多或少:

enter image description here

但是我不能点击它(实际上我可以但它什么都不做,它甚至不会让弹出窗口消失......)。

有人有想法吗?

1 个答案:

答案 0 :(得分:0)

我明白了!

伙计们,永远不要将Checkbox放在Spinner的子视图中。这就是问题所在,如果您删除了Checkbox,它就可以正常使用了!