CheckedTextView勾选ListView中的行没有显示出来勾选、CheckedTextView、ListView

2023-09-12 10:37:00 作者:巧克力的小骄傲ぅ

我有我的ListView排布置的勾号存在问题。在复选标记不显示在列表项被点击,即使在ListView作品(交互工作)。我该如何解决这个问题呢?

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?

推荐答案

您会希望您的自定义行布局是可检查的。

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();
    }
}

在此布局夸大它的外观通过它的孩子一个是可检查(如CheckedTextView或复选框),除了这是很简单的。

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.

下一步使用它的布局:

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

请注意,你不能只是使用CheckableLinearLayout,因为它不是一个内置Android查看你需要告诉编译器在哪里。保存为checkable_list_row.xml,例如。

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);

希望帮助!