具有按钮设置的Android Spinner项目单击侦听器

时间:2014-11-01 21:36:58

标签: java android-layout onclicklistener

我有一个带有自定义ListItem的Spinner,左边有文本,右边有一个按钮。当微调器处于折叠状态时,按钮工作正常。当Spinner展开时,按钮不响应。

ButtonListItemAdapter.java

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent)
    {
        View rowView = convertView;
        if(rowView == null)
        {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.listitem_button, parent, false);
            ButtonListViewHolder viewHolder = new ButtonListViewHolder();
            viewHolder.textView = (TextView) rowView.findViewById(R.id.listitem_button_text1);
            viewHolder.button = (Button) rowView.findViewById(R.id.listitem_button_button1);
            rowView.setTag(viewHolder);
        }

        ButtonListViewHolder holder = (ButtonListViewHolder) rowView.getTag();

        holder.textView.setText(values[position]);
        //Here is where I set the onClickListener
        //I created a class onListButtonClickListener, and that is what
        //I call onClick(convertView, position) on in this method
        holder.button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if(l != null)
                    l.onClick(convertView, position); //l instance of OnListButtonClickListener
            }
        });

        return rowView;
    }

    static class ButtonListViewHolder
    {
        TextView textView;
        Button button;
    }

    public interface OnListButtonClickListener
    {
        public void onClick(View v, int position);
    }

很抱歉,我没有时间发布很多代码。在此先感谢!!

P.S。有谁知道我的其他帐户去了哪里? :d

1 个答案:

答案 0 :(得分:0)

解决了,我正在思考它。我覆盖getDropDownView()并实现它与我实现getView()的方式完全相同。

    @Override
    public View getDropDownView(final int position, final View convertView, ViewGroup parent)
    {
        View rowView = convertView;
        if(rowView == null)
        {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.listitem_button, parent, false);
            ButtonListViewHolder viewHolder = new ButtonListViewHolder();
            viewHolder.textView = (TextView) rowView.findViewById(R.id.listitem_button_text1);
            viewHolder.button = (Button) rowView.findViewById(R.id.listitem_button_button1);
            rowView.setTag(viewHolder);
        }

        ButtonListViewHolder holder = (ButtonListViewHolder) rowView.getTag();

        holder.textView.setText(values[position]);
        //Here is where I set the onClickListener
        //I created a class onListButtonClickListener, and that is what
        //I call onClick(convertView, position) on in this method
        holder.button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if(l != null)
                    l.onClick(convertView, position); //l instance of OnListButtonClickListener
            }
        });

        return rowView;
    }