2

I'm trying to create a list with buttons and control the pressed state of those buttons programatically.

I've created a Selector for those buttons:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/drw_blue_logincombination_pin_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/drw_blue_logincombination_pin_normal" android:state_pressed="false"/>
</selector> 

And I use onTouch events to update them with the setPressed(boolean) function. That part works well.

But in the BaseAdapter I do:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = convertView;
        ViewHolder holder;

        if (ret == null) { // Inflate the view and set attributes
            ret = ViewHelper.inflateNewViewById(activity, R.layout.row_login_combination);
            holder = new ViewHolder();

            holder.delete = (ImageView) ret.findViewById(R.id.btnDelete);
            holder.pin = (ImageView) ret.findViewById(R.id.btnPin);

            holder.pin.setOnTouchListener(holder.listener);

            holder.delete.setOnClickListener(holder.deleteListener);

            ret.setTag(holder);
        } else {
            holder = (ViewHolder) ret.getTag();
        }

        CombinationHelper comb = combs.getCombination(position);

        holder.listener.setPosition(position);
        holder.deleteListener.setPosition(position);

        holder.pin.setPressed(comb.pin);

        return ret;

    }

When I enter the activity and comb.pin == true, the button is setted as pressed but only for a second, then it is set back as not pressed (without passing through the onTouch listener).

But if I create a second row, the first one is updated and then it shows the correct state (pressed).

Maybe I'm making some basic mistake, but I cannot see why it's not working, can anybody help me?

Thanks in advance.

EDIT: This is how the button is declared in the adapter row layout:

<ImageButton
            android:id="@+id/btnPin"
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:background="@drawable/button_pin"
            android:textAppearance="?android:attr/textAppearanceLarge" />
4

0 回答 0