android ListView项目点击闪烁bug



我的问题:当我点击ListViews'项目时,它改变了它的'状态如下顺序:如果未选中-未选中,已选中,未选中,已选中,而我期望它只是被选中,没有任何闪烁和闪烁

if checked: checked - Unchecked(没有问题)

此错误仅在pre-HoneyComb设备上出现在ICS上一切正常

我认为这个bug是指Android的内置机制。如果是这样,有什么解决办法吗?


1) Listview (multiChoice = true)
2)选择器:

    <item  android:drawable="@color/main_listview_item_pressed_longpressed"
        android:state_pressed="true" />
    <item  android:drawable="@color/main_listview_item_pressed_longpressed"
         android:state_checked="true"  />

应用于ListViews的项目布局

3)项目布局:

<com.sasha.medclock2.CheckedLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingLeft="5dip"
        android:background="@drawable/list_item_selector"
        android:id="@+id/linerar_layout_checkable"
         >
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@drawable/textview_selector"
             />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@drawable/textview_selector"
             />
        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@drawable/textview_selector"
             />
        <TextView
            android:id="@+id/text4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@drawable/textview_selector"
             />
    </com.sasha.medclock2.CheckedLinearLayout>
4) CheckedLinearLayout:
public class CheckedLinearLayout extends LinearLayout implements Checkable {
    private final static String TAG = "CheckableLinearLayout";
    private static final int[] CHECKED_STATE_SET = {
        android.R.attr.state_checked
    };
    private boolean checked = false;

    public CheckedLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CheckedLinearLayout(Context context) {
        super(context);
    }
    @Override
    public boolean isChecked() {
        return checked;
    }
    @Override
    public void setChecked(boolean checked) {
        this.checked = checked;
        refreshDrawableState();
        //Propagate to childs
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if(child instanceof Checkable) {
                ((Checkable)child).setChecked(checked);
            }
        }
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }
    @Override
    public void toggle() {
        this.checked = !this.checked;
    }
}

5)我正在使用ActionBarSherlock lib以及HoloEverywhere

6)我检查ListViews的项目与ListView.setItemChecked(…)

非常感谢您的帮助

从我的经验ListView回收项目。你的listAdapter应该保留一个已检查项的列表,并在那里设置了setChecked:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater layoutInflater = context.getLayoutInflater();
        view = layoutInflater.inflate(R.layout.alerts_list_line, null);
    }

    CheckBox cb = (CheckBox) view.findViewById(R.id.cb_list_line);
    cb.setTag(id);
    List<String> list = .getFavoriteId();
    if (list .contains(String.valueOf(info.getId())))
        cb.setChecked(true);
    else
        cb.setChecked(false);
    return view;
}

最新更新