我在使用StateListDrawable作爲背景時有一些非常奇怪的ListView行爲。我試圖按照this後的回答,因爲我沒有得到state_checked狀態,但現在我的ListView變得瘋狂了。ListView項目和狀態選擇器背景的奇怪行爲可繪製
當我點擊一個項目時,它不會立即改變顏色到選擇器中的state_checked項目。儘管點擊了一下,許多視圖會突然切換到state_checked背景。這看起來是隨機的。
這裏是我的狀態選擇XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/grey"
android:endColor="@color/darkgrey"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="@color/orange4"
android:startColor="@color/orange5"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
<item android:state_checked="true">
<shape>
<gradient
android:endColor="@color/brown2"
android:startColor="@color/brown1"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
<item android:state_selected="true">
<shape>
<gradient
android:endColor="@color/brown2"
android:startColor="@color/brown1"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="@color/white"
android:endColor="@color/white2"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/grey05" />
<corners
android:radius="0dp" />
<padding
android:left="10sp"
android:top="10sp"
android:right="10sp"
android:bottom="10sp" />
</shape>
</item>
</selector>
這裏是我爲我的自定義視圖的.java類實現可檢驗:
public class Entry extends LinearLayout implements Checkable {
public Entry(Context context) {
super(context, null);
// Inflate this view
LayoutInflater temp = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
temp.inflate(R.layout.entry, this, true);
initViews();
}
private static final int[] CheckedStateSet = {
android.R.attr.state_checked
};
private void initViews() {
this.setBackgroundResource(R.drawable.listview_row);
}
public boolean isChecked() {
return _checked;
}
public void toggle() {
_checked = !_checked;
}
public void setChecked(boolean checked) {
_checked = checked;
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CheckedStateSet);
}
return drawableState;
}
@Override
public boolean performClick() {
toggle();
return super.performClick();
}
}
我周圍戳了幾個小時嘗試弄清楚,但不幸的是必須承認尋求幫助。任何人都可以看到上面的代碼會導致ListView在項目上出現奇怪行爲的錯誤?如果需要,我也可以發佈更多代碼。
我認爲你應該使用查看包裝類 – Nitin