3

my problem : when I click on ListViews' item , it changes its' state in such order: if unchecked - Unchecked, Checked,Unchecked,Checked while I expect it simply become checked without any flashing and winkling

if checked : Checked - Unchecked (no problem)

this bug appears ONLY on pre-HoneyComb devices , on ICS everything is Ok

I think this bug refers to built-in mechanisms of Android . If so, is there any solution?

What I have :

1) Listview ( multiChoice = true )
2) Selector :

    <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"  />

which is applied to ListViews' item layout

3) Item layout:

<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) I am using ActionBarSherlock lib as well as HoloEverywhere

6) I check ListViews' items with ListView.setItemChecked(...)

Many thanks for any help,

1 Answers1

0

From my experience ListView recycle items. Your listAdapter should keep a list of the checked items and setChecked there:

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;
}
baronS
  • 1,074
  • 11
  • 11
  • Already tried this approach. The problem lies deeper. Probably some Android-specific stuff –  May 03 '13 at 11:34