-1

我有StateListdrawablexml

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

    <item android:state_activated="false" android:drawable="@drawable/ic_checked_off" />
    <item android:state_activated="true" android:drawable="@drawable/ic_checked_on" />
    <item android:drawable="@drawable/ic_checked_on" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_checked_off" />

</selector>

我有GridView 图像StateListDrawable,并以编程方式尝试更改可绘制状态

    if(mSelected.contains(photo)){
        view.findViewById(R.id.selector).setActivated(true);
    }

选择改变状态的图像,但是当我点击他的状态没有改变时,对不起,我无法解释我想要什么,我的英语更差

我试着解释一下

如果mSelected.contains(photo) [state - active] -> [checked_on.jpg]当我单击 -> 状态可绘制从第一个开始并忽略我的编程状态更改

==编辑==

适配器项目 --

private View getPhoto(int position, View convertView, ViewGroup parent){
        View view = convertView;
        if(convertView == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.gallery_photo_item, parent, false);
        }

        ImageView mPhoto = (ImageView)view.findViewById(R.id.photoview);

        Photo photo = (Photo) getItem(position - COLUMNS_NUM);

        if(mSelected.contains(photo)){
            view.findViewById(R.id.selector).setActivated(true); // here i change my drawable state because it's front of my imageView
        }

        mLoader.displayImage(MediaStore.Images.Media.EXTERNAL_CONTENT_URI + File.separator + photo.id,
                mPhoto);

        return view;
    }
4

1 回答 1

0

问题是状态的顺序。从上到下检查它们,并使用与情况匹配的第一个。所以这意味着前两个总是被使用。这应该有效:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ic_checked_on"  />
    <item android:state_activated="false" android:drawable="@drawable/ic_checked_off" />
    <item android:state_activated="true" android:drawable="@drawable/ic_checked_on" />
    <item android:drawable="@drawable/ic_checked_off" />
</selector>
于 2013-10-29T14:31:26.243 回答