Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

android - CheckedTextView checkmark in ListView row not showing up

I have a problem with the Checkmark of my ListView row layout. The checkmark doesn't show up when the ListItem is clicked even though the ListView works (the interaction works). How can i fix this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will want to make you custom row layout that is checkable.

First you need to create a custom layout that implements Checkable:

public class CheckableLinearLayout extends LinearLayout implements Checkable {
    private Checkable mCheckable;

    public CheckableLinearLayout(Context context) {
        this(context, null);
    }

    public CheckableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean isChecked() {
        return mCheckable == null ? false : mCheckable.isChecked();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        // Find Checkable child
        int childCount = getChildCount();
        for (int i = 0; i < childCount; ++i) {
            View v = getChildAt(i);
            if (v instanceof Checkable) {
                mCheckable = (Checkable) v;
                break;
            }
        }
    }

    @Override
    public void setChecked(boolean checked) {
        if(mCheckable != null)
            mCheckable.setChecked(checked);
    }

    @Override
    public void toggle() {
        if(mCheckable != null)
            mCheckable.toggle();
    }
}

After this layout is inflated it looks through it's children for one that is checkable (like a CheckedTextView or CheckBox), other than that it is quite simple.

Next use it in a layout:

<your.package.name.CheckableLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical" >

    <TextView 
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckedTextView 
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/textCheckMark"
        android:gravity="center_vertical"
        android:paddingLeft="6dp"
        android:paddingRight="6dp" />

</your.package.name.CheckableLinearLayout>

Notice that you cannot just use CheckableLinearLayout, since it is not a built-in Android View you need tell the compiler where it is. Save this as checkable_list_row.xml, for example.

Lastly use this new layout like you would with any other custom layout.

adapter = new MySimpleCursorAdapter(this, R.layout.checkable_list_row, cursor,
        new String[] { Database.KEY_DATE , Database.KEY_NAME },
        new int[] {R.id.text1, R.id.text2}, 0);

Hope that helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...