setItemChecked (int position, boolean value) not working?

邮差的信 提交于 2019-12-05 05:37:35

I'm afraid that it is no easy way to do that in the Android Framework.

In order to get the setSelection(...) working, your View has to implement the follogin interface: android.widget.Checkable

You probably are using a some layout for View (an image and 2 textview in a LinearLayout maybe?), which doesn't implement the Checkable interface.

What you can do, is to create a custom View class which implements Checkable.

Check out the link below for a checkable LinearLayout:

http://tokudu.com/2010/android-checkable-linear-layout/


If you want to change the background, than rewrite the setChecked method to do what you want. Very simple example:

@Override
public void setChecked(boolean checked) {
    if (checked) {
        this.setBackgroundColor(Color.RED);
    } else {
        setBackgroundColor(Color.BLACK);
    }
}

set for your list row background selector, which has resource for state_activated:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_activated="true" android:state_enabled="true" android:drawable="@android:color/black"></item>
    <item android:drawable="@android:color/transparent" android:state_enabled="true"/>

</selector>

try including the android:state_enabled attribute as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!