android ListView项目点击闪烁的bug

时间:2013-01-01 13:51:43

标签: android listview actionbarsherlock android-holo-everywhere

我的问题:当我点击ListViews'项目,它改变了它的'以这样的顺序状态:如果未选中 - 未经检查,已选中,未选中,已检查,而我预计它会在没有任何闪烁和闪烁的情况下进行检查

如果选中:已选中 - 未选中(没问题)

此错误出现仅适用于预装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&#39;项目布局

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&#39; ListView.setItemChecked(...)

的项目

非常感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

根据我的经验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;
}