2

I have made a custom RelativeLayout that implements checkable for a ListView. I'm experiencing an error that occurs only on my Gingerbread running HTC Desire and NOT my JellyBean running Galaxy Nexus.

Within the custom RelativeLayout, I override the setChecked method as follows:

public class CustomRL extends RelativeLayout implements Checkable {

private boolean isChecked;
List<RelativeLayout> rl = new ArrayList<RelativeLayout>();
Context context;

public CustomRL(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}

@Override
public boolean isChecked() {
return isChecked;
}

@Override
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
    this.setBackgroundColor(isChecked ? Color.parseColor("#33b5e5")
            : 0x00000000);
}
}

Within my onItemClickListener, I see if the item isChecked via the method above, casting the view as customRL as follows:

@Override
    public void onItemClick(AdapterView<?> listview, View v, int position,
            long id) {

        boolean isChecked = ((CustomRL) v).isChecked();
}

On my Galaxy Nexus, upon clicking of the item, setChecked runs before isChecked thus the latter returns whether the item has been checked correctly. On my Desire however, isChecked runs before setChecked, thus it returns the wrong value. This is reflected by the blue selected colour being set correctly on my Nexus and incorrectly on my Desire (when it is checked it isn't blue, and when it isn't checked it is blue)

This is really annoying and I've been wracking my brain for ages trying to work out why there is a difference. Anyone that can help?

4

1 回答 1

0

可能已经晚了,但今天我也遇到了同样的问题,对我来说,甚至 HTC One V(运行 ICS)也表现出同样的行为,Gi​​ngerbread 和 Froyo 设备也遇到了你提到的同样的问题。我通过HandleronItemClick.

伪代码:

@Override
public void onItemClick(AdapterView<?> listview, final View v, final int position, long id) {

    new Handler().post(new Runnable () { 
        // Your code here
        boolean isChecked = ((CustomRL) v).isChecked();
    } );
}
于 2013-08-06T15:09:06.173 回答